List of usage examples for org.apache.http.impl.client DefaultHttpClient getParams
public synchronized final HttpParams getParams()
From source file:simple.crawler.http.HttpClientFactory.java
public static DefaultHttpClient createNewDefaultHttpClient() { ///* w w w. j a v a 2 s .com*/ HttpParams params = new BasicHttpParams(); //Determines the connection timeout HttpConnectionParams.setConnectionTimeout(params, 1 * 60 * 1000); //Determines the socket timeout HttpConnectionParams.setSoTimeout(params, 1 * 60 * 1000); //Determines whether stale connection check is to be used HttpConnectionParams.setStaleCheckingEnabled(params, false); //The Nagle's algorithm tries to conserve bandwidth by minimizing the number of segments that are sent. //When application wish to decrease network latency and increase performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY) //Data will be sent earlier, at the cost of an increase in bandwidth consumption HttpConnectionParams.setTcpNoDelay(params, true); // HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4"); //Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager pm = new PoolingClientConnectionManager(schemeRegistry); // DefaultHttpClient httpclient = new DefaultHttpClient(pm, params); //ConnManagerParams.setMaxTotalConnections(params, MAX_HTTP_CONNECTION); //ConnManagerParams.setMaxConnectionsPerRoute(params, defaultConnPerRoute); //ConnManagerParams.setTimeout(params, 1 * 60 * 1000); httpclient.getParams().setParameter("http.conn-manager.max-total", MAX_HTTP_CONNECTION); ConnPerRoute defaultConnPerRoute = new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 4; } }; httpclient.getParams().setParameter("http.conn-manager.max-per-route", defaultConnPerRoute); httpclient.getParams().setParameter("http.conn-manager.timeout", 1 * 60 * 1000L); httpclient.getParams().setParameter("http.protocol.allow-circular-redirects", true); httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); // HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount > 2) { return false; } if (exception instanceof NoHttpResponseException) { return true; } if (exception instanceof SSLHandshakeException) { return false; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; httpclient.setHttpRequestRetryHandler(retryHandler); HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip, deflate"); } } }; HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header header = entity.getContentEncoding(); if (header != null) { HeaderElement[] codecs = header.getElements(); for (int i = 0; i < codecs.length; i++) { String codecName = codecs[i].getName(); if ("gzip".equalsIgnoreCase(codecName)) { response.setEntity(new GzipDecompressingEntity(entity)); return; } else if ("deflate".equalsIgnoreCase(codecName)) { response.setEntity(new DeflateDecompressingEntity(entity)); return; } } } } }; httpclient.addRequestInterceptor(requestInterceptor); httpclient.addResponseInterceptor(responseInterceptor); httpclient.setRedirectStrategy(new DefaultRedirectStrategy()); return httpclient; }
From source file:eu.thecoder4.gpl.pleftdroid.PleftBroker.java
/** * @return the client//from www. jav a 2s. c o m */ protected static DefaultHttpClient getDefaultClient() { // Init HTTP params HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, CONN_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT); httpParameters.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); httpParameters.setParameter("http.protocol.content-charset", "UTF-8"); DefaultHttpClient client = new DefaultHttpClient(httpParameters); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "PleftDroid/1.0 (Linux; U; Android " + Build.VERSION.RELEASE + "/" + Build.VERSION.CODENAME + "; " + Locale.getDefault().getLanguage() + "; " + Build.MODEL + ")"); return client; }
From source file:com.wiyun.engine.network.Network.java
static DefaultHttpClient createHttpClient(int timeout) { // wifi connected? boolean wifi = isWifiConnected(); // create client SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); try {/*from w w w . ja v a 2s . c o m*/ schemeRegistry.register(new Scheme("https", new TrustAllSSLSocketFactory(), 443)); } catch (Exception e) { } HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUseExpectContinue(params, false); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); HttpConnectionParams.setConnectionTimeout(params, wifi ? timeout : (timeout * 3)); HttpConnectionParams.setSoTimeout(params, wifi ? timeout : (timeout * 3)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, schemeRegistry); DefaultHttpClient client = new DefaultHttpClient(ccm, params); if (!wifi && hasProxy()) { HttpHost proxy = getProxy(); if (proxy != null) client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } return client; }
From source file:com.cloudbees.eclipse.core.util.Utils.java
/** * @param url//from w ww .j av a 2s. c o m * url to connec. Required to determine proxy settings if available. If <code>null</code> then proxy is not * configured for the client returned. * @return * @throws CloudBeesException */ public final static DefaultHttpClient getAPIClient(String url) throws CloudBeesException { DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY); String version = null; if (CloudBeesCorePlugin.getDefault() != null) { version = CloudBeesCorePlugin.getDefault().getBundle().getVersion().toString(); } else { version = "n/a"; } HttpProtocolParams.setUserAgent(httpclient.getParams(), "CBEclipseToolkit/" + version); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); CloudBeesCorePlugin plugin = CloudBeesCorePlugin.getDefault(); URL truststore; if (plugin == null) { //Outside the OSGI environment, try to open the stream from the current dir. truststore = new File("truststore").toURI().toURL(); } else { truststore = plugin.getBundle().getResource("truststore"); } InputStream instream = truststore.openStream(); try { trustStore.load(instream, "123456".toCharArray()); } finally { instream.close(); } TrustStrategy trustAllStrategy = new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }; SSLSocketFactory socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, null, null, trustStore, null, trustAllStrategy, SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); // Override https handling to use provided truststore @SuppressWarnings("deprecation") Scheme sch = new Scheme("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch); HttpParams params = httpclient.getParams(); //TODO Make configurable from the UI? HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); if (CloudBeesCorePlugin.getDefault() != null) { // exclude proxy support when running outside eclipse IProxyService ps = CloudBeesCorePlugin.getDefault().getProxyService(); if (ps.isProxiesEnabled()) { IProxyData[] pr = ps.select(new URI(url)); //NOTE! For now we use just the first proxy settings with type HTTP or HTTPS to try out the connection. If configuration has more than 1 conf then for now this likely won't work! if (pr != null) { for (int i = 0; i < pr.length; i++) { IProxyData prd = pr[i]; if (IProxyData.HTTP_PROXY_TYPE.equals(prd.getType()) || IProxyData.HTTPS_PROXY_TYPE.equals(prd.getType())) { String proxyHost = prd.getHost(); int proxyPort = prd.getPort(); String proxyUser = prd.getUserId(); String proxyPass = prd.getPassword(); HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (prd.isRequiresAuthentication()) { List authpref = new ArrayList(); authpref.add(AuthPolicy.BASIC); AuthScope authScope = new AuthScope(proxyHost, proxyPort); httpclient.getCredentialsProvider().setCredentials(authScope, new UsernamePasswordCredentials(proxyUser, proxyPass)); } break; } } } } } /* httpclient.getHostConfiguration().setProxy(proxyHost,proxyPort); //if there are proxy credentials available, set those too Credentials proxyCredentials = null; String proxyUser = beesClientConfiguration.getProxyUser(); String proxyPassword = beesClientConfiguration.getProxyPassword(); if(proxyUser != null || proxyPassword != null) proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword); if(proxyCredentials != null) client.getState().setProxyCredentials(AuthScope.ANY, proxyCredentials); */ return httpclient; } catch (Exception e) { throw new CloudBeesException("Error while initiating access to JSON APIs!", e); } }
From source file:com.serotonin.m2m2.Common.java
public static HttpClient getHttpClient(int timeout) { DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter("http.socket.timeout", timeout); client.getParams().setParameter("http.connection.timeout", timeout); client.getParams().setParameter("http.connection-manager.timeout", timeout); client.getParams().setParameter("http.protocol.head-body-timeout", timeout); if (SystemSettingsDao.getBooleanValue(SystemSettingsDao.HTTP_CLIENT_USE_PROXY)) { String proxyHost = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_SERVER); int proxyPort = SystemSettingsDao.getIntValue(SystemSettingsDao.HTTP_CLIENT_PROXY_PORT); String username = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_USERNAME, ""); String password = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_PASSWORD, ""); client.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(username, password)); }//from w ww.j a v a2 s. co m return client; }
From source file:com.amazonaws.eclipse.core.HttpClientFactory.java
private static void configureProxy(DefaultHttpClient client, String url) { AwsToolkitCore plugin = AwsToolkitCore.getDefault(); if (plugin != null) { IProxyService proxyService = AwsToolkitCore.getDefault().getProxyService(); if (proxyService.isProxiesEnabled()) { try { IProxyData[] proxyData;// w w w .ja v a 2 s.c o m proxyData = proxyService.select(new URI(url)); if (proxyData.length > 0) { IProxyData data = proxyData[0]; client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(data.getHost(), data.getPort())); if (data.isRequiresAuthentication()) { client.getCredentialsProvider().setCredentials( new AuthScope(data.getHost(), data.getPort()), new NTCredentials(data.getUserId(), data.getPassword(), null, null)); } } } catch (URISyntaxException e) { plugin.getLog().log(new Status(Status.ERROR, AwsToolkitCore.PLUGIN_ID, e.getMessage(), e)); } } } }
From source file:de.incoherent.suseconferenceclient.app.HTTPWrapper.java
public static JSONObject get(String url) throws IllegalStateException, SocketException, UnsupportedEncodingException, IOException, JSONException { HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; DefaultHttpClient client = new DefaultHttpClient(); SchemeRegistry registry = new SchemeRegistry(); SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); registry.register(new Scheme("https", socketFactory, 443)); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry); DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams()); HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); HttpGet get = new HttpGet(url); HttpResponse response = httpClient.execute(get); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode >= 200 && statusCode <= 299) { return HTTPWrapper.parseResponse(response); } else {//from w w w. java2 s . c o m throw new HttpResponseException(statusCode, statusLine.getReasonPhrase()); } }
From source file:biocode.fims.ezid.EzidService.java
/** * Generate an HTTP Client for communicating with web services that is * thread safe and can be used in the context of a multi-threaded application. * * @return DefaultHttpClient/* ww w .j a v a2 s . c o m*/ */ private static DefaultHttpClient createThreadSafeClient() { DefaultHttpClient client = new DefaultHttpClient(); ClientConnectionManager mgr = client.getConnectionManager(); HttpParams params = client.getParams(); ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(mgr.getSchemeRegistry()); connManager.setDefaultMaxPerRoute(CONNECTIONS_PER_ROUTE); client = new DefaultHttpClient(connManager, params); return client; }
From source file:com.amazonaws.util.HttpUtils.java
/** * Fetches a file from the URI given and returns an input stream to it. * * @param uri the uri of the file to fetch * @param config optional configuration overrides * @return an InputStream containing the retrieved data * @throws IOException on error/*w w w .j av a 2 s. c o m*/ */ public static InputStream fetchFile(final URI uri, final ClientConfiguration config) throws IOException { HttpParams httpClientParams = new BasicHttpParams(); HttpProtocolParams.setUserAgent(httpClientParams, getUserAgent(config)); HttpConnectionParams.setConnectionTimeout(httpClientParams, getConnectionTimeout(config)); HttpConnectionParams.setSoTimeout(httpClientParams, getSocketTimeout(config)); DefaultHttpClient httpclient = new DefaultHttpClient(httpClientParams); if (config != null) { String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (config.getProxyUsername() != null && config.getProxyPassword() != null) { httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(config.getProxyUsername(), config.getProxyPassword(), config.getProxyWorkstation(), config.getProxyDomain())); } } } HttpResponse response = httpclient.execute(new HttpGet(uri)); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException("Error fetching file from " + uri + ": " + response); } return new HttpClientWrappingInputStream(httpclient, response.getEntity().getContent()); }
From source file:org.commonjava.redhat.maven.rv.util.InputUtils.java
public static File getFile(final String location, final File downloadsDir, final boolean deleteExisting) throws ValidationException { if (client == null) { final DefaultHttpClient hc = new DefaultHttpClient(); hc.setRedirectStrategy(new DefaultRedirectStrategy()); final String proxyHost = System.getProperty("http.proxyHost"); final int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "-1")); if (proxyHost != null && proxyPort > 0) { final HttpHost proxy = new HttpHost(proxyHost, proxyPort); hc.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy); }//ww w . java 2 s .c o m client = hc; } File result = null; if (location.startsWith("http")) { LOGGER.info("Downloading: '" + location + "'..."); try { final URL url = new URL(location); final String userpass = url.getUserInfo(); if (!isEmpty(userpass)) { final AuthScope scope = new AuthScope(url.getHost(), url.getPort()); final Credentials creds = new UsernamePasswordCredentials(userpass); client.getCredentialsProvider().setCredentials(scope, creds); } } catch (final MalformedURLException e) { LOGGER.error("Malformed URL: '" + location + "'", e); throw new ValidationException("Failed to download: %s. Reason: %s", e, location, e.getMessage()); } final File downloaded = new File(downloadsDir, new File(location).getName()); if (deleteExisting && downloaded.exists()) { downloaded.delete(); } if (!downloaded.exists()) { HttpGet get = new HttpGet(location); OutputStream out = null; try { HttpResponse response = client.execute(get); // Work around for scenario where we are loading from a server // that does a refresh e.g. gitweb if (response.containsHeader("Cache-control")) { LOGGER.info("Waiting for server to generate cache..."); try { Thread.sleep(5000); } catch (final InterruptedException e) { } get.abort(); get = new HttpGet(location); response = client.execute(get); } final int code = response.getStatusLine().getStatusCode(); if (code == 200) { final InputStream in = response.getEntity().getContent(); out = new FileOutputStream(downloaded); copy(in, out); } else { LOGGER.info(String.format("Received status: '%s' while downloading: %s", response.getStatusLine(), location)); throw new ValidationException("Received status: '%s' while downloading: %s", response.getStatusLine(), location); } } catch (final ClientProtocolException e) { throw new ValidationException("Failed to download: '%s'. Error: %s", e, location, e.getMessage()); } catch (final IOException e) { throw new ValidationException("Failed to download: '%s'. Error: %s", e, location, e.getMessage()); } finally { closeQuietly(out); get.abort(); } } result = downloaded; } else { LOGGER.info("Using local file: '" + location + "'..."); result = new File(location); } return result; }