List of usage examples for org.apache.http.client HttpClient getParams
@Deprecated HttpParams getParams();
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
/** * HTTP?// w w w. jav a2s .co m * <p> * ? HedaerContent-Type * </p> * HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Content-Type", * "text/html;charset=UTF-8"); * * * <p> * ? HttpClientCONTENT_CHARSET * </p> * HttpClient httpClient = new DefaultHttpClient(); * httpClient.getParams().setParameter( * CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); * * * <p> * ? get/post methodCONTENT_CHARSET * </p> * HttpGet httpGet = new HttpGet(url); * httpGet.getParams().setParameter(CoreProtocolPNames. * HTTP_CONTENT_CHARSET, "UTF-8"); * * @throws Exception */ @Test public void testHttpGet() throws Exception { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("https://github.com/alibaba/dubbo/archive/dubbo-2.4.11.zip"); httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); String content = parseEntity(entity); System.out.println(content); }
From source file:org.wso2.developerstudio.appcloud.utils.client.HttpsJaggeryClient.java
@SuppressWarnings("deprecation") public static HttpClient wrapClient(HttpClient base, String urlStr) { try {/* w w w . ja va 2s . c om*/ SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = new ThreadSafeClientConnManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); URL url = new URL(urlStr); int port = url.getPort(); if (port == -1) { port = 443; } String protocol = url.getProtocol(); if ("https".equals(protocol)) { if (port == -1) { port = 443; } } else if ("http".equals(protocol)) { if (port == -1) { port = 80; } } sr.register(new Scheme(protocol, ssf, port)); return new DefaultHttpClient(ccm, base.getParams()); } catch (Throwable ex) { ex.printStackTrace(); log.error("Trust Manager Error", ex); return null; } }
From source file:custom.application.login.java
public String oAuth2callback() throws ApplicationException { HttpServletRequest request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST"); HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE"); Reforward reforward = new Reforward(request, response); TokenResponse oauth2_response;//from w w w . j av a 2 s . c o m try { if (this.getVariable("google_client_secrets") == null) { clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(login.class.getResourceAsStream("/clients_secrets.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ "); } this.setVariable(new ObjectVariable("google_client_secrets", clientSecrets)); } else clientSecrets = (GoogleClientSecrets) this.getVariable("google_client_secrets").getValue(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, SCOPES).build(); oauth2_response = flow.newTokenRequest(request.getParameter("code")) .setRedirectUri(this.getLink("oauth2callback")).execute(); System.out.println("Ok:" + oauth2_response.toPrettyString()); } catch (IOException e1) { // TODO Auto-generated catch block throw new ApplicationException(e1.getMessage(), e1); } catch (GeneralSecurityException e) { // TODO Auto-generated catch block throw new ApplicationException(e.getMessage(), e); } try { HttpClient httpClient = new DefaultHttpClient(); String url = "https://www.google.com/m8/feeds/contacts/default/full"; url = "https://www.googleapis.com/oauth2/v1/userinfo"; HttpGet httpget = new HttpGet(url + "?access_token=" + oauth2_response.getAccessToken()); httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8"); HttpResponse http_response = httpClient.execute(httpget); HeaderIterator iterator = http_response.headerIterator(); while (iterator.hasNext()) { Header next = iterator.nextHeader(); System.out.println(next.getName() + ":" + next.getValue()); } InputStream instream = http_response.getEntity().getContent(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len = instream.read(bytes)) != -1) { out.write(bytes, 0, len); } instream.close(); out.close(); Struct struct = new Builder(); struct.parse(new String(out.toByteArray(), "utf-8")); this.usr = new User(); this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue()); if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) { usr.setPassword(""); usr.setUsername(usr.getEmail()); usr.setLastloginIP(request.getRemoteAddr()); usr.setLastloginTime(new Date()); usr.setRegistrationTime(new Date()); usr.append(); } new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId()); reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8")); reforward.forward(); return new String(out.toByteArray(), "utf-8"); } catch (ClientProtocolException e) { throw new ApplicationException(e.getMessage(), e); } catch (IOException e) { throw new ApplicationException(e.getMessage(), e); } catch (ParseException e) { throw new ApplicationException(e.getMessage(), e); } }
From source file:org.string_db.psicquic.ws.StringdbSolrBasedPsicquicRestService.java
protected HttpClient createHttpClient() { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(maxTotalConnections); cm.setDefaultMaxPerRoute(defaultMaxConnectionsPerHost); HttpClient httpClient = new DefaultHttpClient(cm); String proxyHost = config.getProxyHost(); String proxyPort = config.getProxyPort(); if (isValueSet(proxyHost) && proxyHost.trim().length() > 0 && isValueSet(proxyPort) && proxyPort.trim().length() > 0) { try {/*from w w w. j av a 2s.c o m*/ HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort)); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } catch (Exception e) { logger.error("Impossible to create proxy host:" + proxyHost + ", port:" + proxyPort, e); } } return httpClient; }
From source file:com.android.aft.AFNetworkConnection.AFNetworkConnection.java
/** * Configure the using http client// w w w . ja va 2 s . c o m * * @param client */ private void configureHttpClient(HttpClient client) { final HttpParams httpParameters = client.getParams(); if (CONNECTION_TIMEOUT > 0) HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT); if (SOCKET_TIMEOUT > 0) HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT); }
From source file:com.life.wuhan.util.ImageDownloader.java
private Bitmap downloadBitmap(final String imageUrl) { // AndroidHttpClient is not allowed to be used from the main thread final HttpClient client = new DefaultHttpClient(); // ??//from w w w . j a v a 2s . c o m if (NetworkUtil.getNetworkType(mContext) == NetworkUtil.APN_CMWAP) { HttpHost proxy = new HttpHost(NetworkUtil.getHostIp(), NetworkUtil.getHostPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } final HttpGet getRequest = new HttpGet(imageUrl); try { HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { final byte[] respBytes = EntityUtils.toByteArray(entity); writeImageFile(imageUrl, entity, respBytes); // Decode the bytes and return the bitmap. return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length, null); } } catch (IOException e) { getRequest.abort(); } catch (OutOfMemoryError e) { clearCache(); System.gc(); } catch (IllegalStateException e) { getRequest.abort(); } catch (Exception e) { getRequest.abort(); } finally { } return null; }
From source file:net.sourceforge.subsonic.service.PodcastService.java
@SuppressWarnings({ "unchecked" }) private void doRefreshChannel(PodcastChannel channel, boolean downloadEpisodes) { InputStream in = null;/*w w w .j av a2 s . co m*/ HttpClient client = new DefaultHttpClient(); try { channel.setStatus(PodcastStatus.DOWNLOADING); channel.setErrorMessage(null); podcastDao.updateChannel(channel); HttpConnectionParams.setConnectionTimeout(client.getParams(), 2 * 60 * 1000); // 2 minutes HttpConnectionParams.setSoTimeout(client.getParams(), 10 * 60 * 1000); // 10 minutes HttpGet method = new HttpGet(channel.getUrl()); HttpResponse response = client.execute(method); in = response.getEntity().getContent(); Document document = new SAXBuilder().build(in); Element channelElement = document.getRootElement().getChild("channel"); channel.setTitle(channelElement.getChildTextTrim("title")); channel.setDescription(channelElement.getChildTextTrim("description")); channel.setStatus(PodcastStatus.COMPLETED); channel.setErrorMessage(null); podcastDao.updateChannel(channel); refreshEpisodes(channel, channelElement.getChildren("item")); } catch (Exception x) { LOG.warn("Failed to get/parse RSS file for Podcast channel " + channel.getUrl(), x); channel.setStatus(PodcastStatus.ERROR); channel.setErrorMessage(x.toString()); podcastDao.updateChannel(channel); } finally { IOUtils.closeQuietly(in); client.getConnectionManager().shutdown(); } if (downloadEpisodes) { for (final PodcastEpisode episode : getEpisodes(channel.getId(), false)) { if (episode.getStatus() == PodcastStatus.NEW && episode.getUrl() != null) { downloadEpisode(episode); } } } }
From source file:ro.teodorbaciu.commons.client.ws.util.WebClientDevWrapper.java
/** * Provides a new instance of http client that wraps the * instance specified as parameter.//from w w w . j av a2s . c om */ @SuppressWarnings("deprecation") public static DefaultHttpClient wrapClient(HttpClient base) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException { } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier verifier = new X509HostnameVerifier() { @Override public void verify(String string, SSLSocket ssls) throws IOException { } @Override public void verify(String string, X509Certificate xc) throws SSLException { } @Override public void verify(String string, String[] strings, String[] strings1) throws SSLException { } @Override public boolean verify(String string, SSLSession ssls) { return true; } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); ssf.setHostnameVerifier(verifier); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", ssf, 443)); return new DefaultHttpClient(ccm, base.getParams()); } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:com.secretlisa.lib.utils.BaseImageLoader.java
public Bitmap downloadBitmap(final String imageUrl) { // AndroidHttpClient is not allowed to be used from the main thread final HttpClient client = new DefaultHttpClient(); // ??//from www.jav a2s. c om int netType = NetworkUtil.getNetworkType(mContext); if (netType == NetworkUtil.TYPE_WAP) { String proxyHost = android.net.Proxy.getDefaultHost(); if (proxyHost != null) { HttpHost proxy = new HttpHost(proxyHost, android.net.Proxy.getDefaultPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } } final HttpGet getRequest = new HttpGet(imageUrl); try { HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { try { final byte[] respBytes = getBytes(entity.getContent()); writeImageFile(imageUrl, respBytes); // Decode the bytes and return the bitmap. return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length, null); } finally { entity.consumeContent(); } } } catch (IOException e) { getRequest.abort(); } catch (OutOfMemoryError e) { clearCache(); System.gc(); } catch (IllegalStateException e) { getRequest.abort(); } catch (Exception e) { getRequest.abort(); } finally { } return null; }