List of usage examples for org.apache.http.params HttpParams setParameter
HttpParams setParameter(String str, Object obj);
From source file:SandBox.testing.PageFetcher.java
public PageFetcher(CrawlConfig config) { super(config); HttpParams params = new BasicHttpParams(); HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params); paramsBean.setVersion(HttpVersion.HTTP_1_1); paramsBean.setContentCharset("UTF-8"); paramsBean.setUseExpectContinue(false); params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString()); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout()); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout()); params.setBooleanParameter("http.protocol.handle-redirects", false); SSLContext sslContext = null; try {/*w w w.j a va2s . c o m*/ sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers ============="); return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted ============="); } public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme("https", 443, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); //SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); if (config.isIncludeHttpsPages()) { schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); } connectionManager = new PoolingClientConnectionManager(schemeRegistry); connectionManager.setMaxTotal(config.getMaxTotalConnections()); connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); httpClient = new DefaultHttpClient(connectionManager, params); if (config.getProxyHost() != null) { if (config.getProxyUsername() != null) { httpClient.getCredentialsProvider().setCredentials( new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword())); } HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort()); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) { HeaderElement[] codecs = contentEncoding.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); if (connectionMonitorThread == null) { connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager); } connectionMonitorThread.start(); }
From source file:anhttpclient.impl.DefaultWebBrowser.java
private HttpParams getBasicHttpParams() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8); params.setParameter(CoreProtocolPNames.USER_AGENT, WebBrowserConstants.DEFAULT_USER_AGENT); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout); params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false); params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, clientConnectionFactoryClassName); /*Custom parameter to be used in implementation of {@link ClientConnectionManagerFactory}*/ params.setParameter(ClientConnectionManagerFactoryImpl.THREAD_SAFE_CONNECTION_MANAGER, this.threadSafe); return params; }
From source file:net.dahanne.gallery.g2.java.client.business.G2Client.java
private DefaultHttpClient createHttpClient() { // avoid instanciating SchemeRegistry schemeRegistry = new SchemeRegistry(); // http scheme schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // https scheme schemeRegistry.register(new Scheme("https", new FakeSocketFactory(), 443)); HttpParams params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); return new DefaultHttpClient(cm, params); }
From source file:edu.uci.ics.crawler4j.crawler.fetcher.PageFetcher.java
public PageFetcher(ICrawlerSettings config) { politenessDelay = config.getPolitenessDelay(); maxDownloadSize = config.getMaxDownloadSize(); show404Pages = config.getShow404Pages(); ignoreBinary = !config.getIncludeBinaryContent(); cache = config.getCacheProvider();/*from w w w .j a va2 s . co m*/ HttpParams params = new BasicHttpParams(); HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params); paramsBean.setVersion(HttpVersion.HTTP_1_1); paramsBean.setContentCharset("UTF-8"); paramsBean.setUseExpectContinue(false); params.setParameter("http.useragent", config.getUserAgent()); params.setIntParameter("http.socket.timeout", config.getSocketTimeout()); params.setIntParameter("http.connection.timeout", config.getConnectionTimeout()); params.setBooleanParameter("http.protocol.handle-redirects", false); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); if (config.getAllowHttps()) { schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); } connectionManager = new ThreadSafeClientConnManager(schemeRegistry); connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); connectionManager.setMaxTotal(config.getMaxTotalConnections()); logger.setLevel(Level.INFO); httpclient = new DefaultHttpClient(connectionManager, params); }
From source file:com.foundstone.certinstaller.CertInstallerActivity.java
/** * Build a connection using the custom Trust Manager and Socket Factory to * grab the certificates./*from ww w . jav a 2 s . c o m*/ * * @param urlString * @param proxyIP * @param proxyPort */ public void grabCerts(final String urlString, final String proxyIP, final String proxyPort) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry .register(new Scheme("https", new EasySSLSocketFactory(CertInstallerActivity.this), 443)); HttpParams httpParams = new BasicHttpParams(); httpParams.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); httpParams.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); httpParams.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); Log.d(TAG, proxyIP); Log.d(TAG, proxyPort); httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyIP, Integer.parseInt(proxyPort))); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); ClientConnectionManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); DefaultHttpClient defaultClient = new DefaultHttpClient(cm, httpParams); HttpGet httpget = new HttpGet(); try { httpget.setURI(new URI("https://" + urlString)); Log.d(TAG, urlString); HttpResponse httpResponse = defaultClient.execute(httpget); InputStream in = httpResponse.getEntity().getContent(); // Once this connection is made the certs are grabbed BufferedReader reader = new BufferedReader(new InputStreamReader(in)); in.close(); } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(); }
From source file:com.stepsdk.android.api.APIClient.java
public HttpEntity httpPost(String url, UrlEncodedFormEntity ent) throws NetworkDownException, HttpPostException { mHttpclient = new DefaultHttpClient(); ClientConnectionManager mgr = mHttpclient.getConnectionManager(); HttpParams params = mHttpclient.getParams(); if (WEB_USER_AGENT != null) params.setParameter(CoreProtocolPNames.USER_AGENT, WEB_USER_AGENT); mHttpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);/*from www .ja v a 2s. com*/ HttpPost post = new HttpPost(url); if (ent != null) post.setEntity(ent); try { if (!NetworkUtil.isOnline(mContext)) throw new NetworkDownException(); HttpResponse response = mHttpclient.execute(post); return response.getEntity(); } catch (ClientProtocolException e) { throw new HttpPostException(e.getMessage()); } catch (IOException e) { throw new HttpPostException(e.getMessage()); } }
From source file:com.stepsdk.android.api.APIClient.java
private HttpEntity httpPost(String url, MultipartEntity mpEntity) throws NetworkDownException, HttpPostException { mHttpclient = new DefaultHttpClient(); ClientConnectionManager mgr = mHttpclient.getConnectionManager(); HttpParams params = mHttpclient.getParams(); if (WEB_USER_AGENT != null) params.setParameter(CoreProtocolPNames.USER_AGENT, WEB_USER_AGENT); int timeoutConnection = 3000; HttpConnectionParams.setConnectionTimeout(params, timeoutConnection); int timeoutSocket = 5000; HttpConnectionParams.setSoTimeout(params, timeoutSocket); mHttpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);// ww w. ja va 2 s .c om HttpPost httppost = new HttpPost(url); httppost.setEntity(mpEntity); try { if (!NetworkUtil.isOnline(mContext)) throw new NetworkDownException(); log(TAG, "executing request " + httppost.getRequestLine()); HttpResponse response = mHttpclient.execute(httppost); log(TAG, response.getStatusLine().toString()); HttpEntity result = response.getEntity(); if (response != null) { log(TAG, EntityUtils.toString(result)); result.consumeContent(); } mHttpclient.getConnectionManager().shutdown(); return result; } catch (ClientProtocolException e) { mHttpclient.getConnectionManager().shutdown(); throw new HttpPostException(e.getMessage()); } catch (IOException e) { mHttpclient.getConnectionManager().shutdown(); throw new HttpPostException(e.getMessage()); } }
From source file:cn.tiup.httpproxy.ProxyServlet.java
@Override public void init() throws ServletException { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); }//from w ww . j ava 2 s. co m String doForwardIPString = getConfigParam(P_FORWARDEDFOR); if (doForwardIPString != null) { this.doForwardIP = Boolean.parseBoolean(doForwardIPString); } initTarget();//sets target* HttpParams hcParams = new BasicHttpParams(); hcParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); hcParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); // See #70 readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class); proxyClient = createHttpClient(hcParams); }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@SuppressWarnings("deprecation") @Test/*from w w w.j a va 2 s. c om*/ public void testSSLConnection() throws Exception { Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80); SSLSocketFactory ssf = new SSLSocketFactory(SSLContext.getInstance("TLS")); ssf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); Scheme https = new Scheme("https", ssf, 443); SchemeRegistry sr = new SchemeRegistry(); sr.register(http); sr.register(https); TrustManager easyTrustManager = new X509TrustManager() { public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) { System.out.println("checkClientTrusted"); } public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) { System.out.println("checkServerTrusted"); } public java.security.cert.X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers"); return null; } }; SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { easyTrustManager }, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext); SSLSocket socket = (SSLSocket) sf.createSocket(); socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" }); HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); sf.connectSocket(socket, "119.29.234.42", 443, null, -1, params); }