protected CookieSpecRegistry createCookieSpecRegistry() { CookieSpecRegistry registry = new CookieSpecRegistry(); registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); registry.register( CookiePolicy.IGNORE_COOKIES, new IgnoreSpecFactory()); return registry; }
protected CookieSpecRegistry createCookieSpecRegistry() { final CookieSpecRegistry registry = new CookieSpecRegistry(); registry.register( CookieSpecs.DEFAULT, new BestMatchSpecFactory()); registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); registry.register( CookiePolicy.IGNORE_COOKIES, new IgnoreSpecFactory()); return registry; }
@Override public void init() throws ServletException { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); } 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); readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class); proxyClient = createHttpClient(hcParams); }
@Override public void init() throws ServletException { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); } 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); readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class); proxyClient = createHttpClient(hcParams); }
/** * 获取DefaultHttpClient对象 * * @param charset * 字符编码 * @return DefaultHttpClient对象 */ private static DefaultHttpClient getDefaultHttpClient(final String charset) { DefaultHttpClient httpclient = new DefaultHttpClient(); // 模拟浏览器,解决一些服务器程序只允许浏览器访问的问题 httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); httpclient.getParams().setParameter( CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); httpclient.getParams().setParameter( CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset == null ? CHARSET_ENCODING : charset); // 浏览器兼容性 httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // 定义重试策略 httpclient.setHttpRequestRetryHandler(requestRetryHandler); return httpclient; }
@Override protected CookieSpecRegistry createCookieSpecRegistry() { CookieSpecRegistry registry = new CookieSpecRegistry(); registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); return registry; }
@SuppressWarnings("deprecation") @Test public void testCookiePolicy() { SpringClientFactory factory = new SpringClientFactory(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); addEnvironment(parent, "ribbon.restclient.enabled=true"); parent.register(RibbonAutoConfiguration.class, ArchaiusAutoConfiguration.class); parent.refresh(); factory.setApplicationContext(parent); RestClient client = factory.getClient("foo", RestClient.class); ApacheHttpClient4 jerseyClient = (ApacheHttpClient4) client.getJerseyClient(); assertEquals(CookiePolicy.IGNORE_COOKIES, jerseyClient.getClientHandler() .getHttpClient().getParams().getParameter(ClientPNames.COOKIE_POLICY)); parent.close(); factory.destroy(); }
public synchronized HttpClient getClient() { if (m_client == null) { m_client = new DefaultHttpClient(); HttpParams clientParams = m_client.getParams(); clientParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, m_timeout); clientParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, m_timeout); clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); m_client.setParams(clientParams); m_client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(m_retries, false)); } return m_client; }
private static HttpParams buildParams(final HttpCollectionSet collectionSet) { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, computeVersion(collectionSet.getUriDef())); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.parseInt(ParameterMap.getKeyedString(collectionSet.getParameters(), "timeout", DEFAULT_SO_TIMEOUT))); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.parseInt(ParameterMap.getKeyedString(collectionSet.getParameters(), "timeout", DEFAULT_SO_TIMEOUT))); params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); //review the httpclient code, looks like virtual host is checked for null //and if true, sets Host to the connection's host property String virtualHost = collectionSet.getUriDef().getUrl().getVirtualHost(); if (virtualHost != null) { params.setParameter( ClientPNames.VIRTUAL_HOST, new HttpHost(virtualHost, collectionSet.getPort()) ); } return params; }
private ConnectionHelper() { HttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS); ConnPerRouteBean connPerRoute = new ConnPerRouteBean( MAX_CONNECTIONS_PER_HOST); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(httpParams, false); SchemeRegistry reg = new SchemeRegistry(); reg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); reg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager( httpParams, reg); HttpConnectionParams.setConnectionTimeout(httpParams, CON_TIME_OUT_MS); HttpConnectionParams.setSoTimeout(httpParams, SO_TIME_OUT_MS); HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.BROWSER_COMPATIBILITY); httpClient = new DefaultHttpClient(connectionManager, httpParams); }
@Test @DatabaseSetup("/database_seed.xml") public void the_approval_will_be_remembered() throws IOException { givenValidAuthCode("marissa", "koala", "internal"); givenAuthCode(); givenAccessTokenUsingAuthCode(); { HttpGet httpGet = new HttpGet(loginUri); httpGet.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.NETSCAPE); httpGet.getParams().setBooleanParameter("http.protocol.handle-redirects", false); authCodeResponse = httpClient.execute(httpGet); httpGet.releaseConnection(); } givenAuthCode(); givenAccessTokenUsingAuthCode(); assertTrue(accessToken != null); assertNotNull(accessToken.getRefreshToken()); }
@Test @DatabaseSetup("/database_seeds/LoginOAuth2IT/database_seed_zero_validity.xml") public void the_approval_will_not_be_remembered_if_validity_is_zero() throws IOException { givenValidAuthCode("marissa", "koala", "internal"); givenAuthCode(); givenAccessTokenUsingAuthCode(); HttpGet httpGet = new HttpGet(loginUri); httpGet.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.NETSCAPE); httpGet.getParams().setBooleanParameter("http.protocol.handle-redirects", false); authCodeResponse = httpClient.execute(httpGet); String response = IOUtils.toString(authCodeResponse.getEntity().getContent()); httpGet.releaseConnection(); assertThat(response, containsString("<title>Access confirmation</title>")); }
public HttpStoreClientFactory(ClientConfig config) { super(config); ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(), config.getConnectionTimeout(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS); mgr.setMaxTotal(config.getMaxTotalConnections()); mgr.setDefaultMaxPerRoute(config.getMaxConnectionsPerNode()); this.httpClient = new DefaultHttpClient(mgr); HttpParams clientParams = this.httpClient.getParams(); HttpProtocolParams.setUserAgent(clientParams, VOLDEMORT_USER_AGENT); HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1); HttpConnectionParams.setConnectionTimeout(clientParams, config.getConnectionTimeout(TimeUnit.MILLISECONDS)); HttpConnectionParams.setSoTimeout(clientParams, config.getSocketTimeout(TimeUnit.MILLISECONDS)); HttpConnectionParams.setStaleCheckingEnabled(clientParams, false); this.httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES); this.reroute = config.getRoutingTier().equals(RoutingTier.SERVER); this.requestFormatFactory = new RequestFormatFactory(); }
private DefaultHttpClient createClient(boolean https) { final DefaultHttpClient client = (https ? new DefaultHttpClient(conMan) : new DefaultHttpClient()); // Allows a slightly lenient cookie acceptance client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // Allows follow of redirects on POST client.setRedirectStrategy(new LaxRedirectStrategy()); return client; }
/** * Setup some variables */ public void setup() { log.info("Setup..."); if (StringUtils.isEmpty(this.username)) { throw new IllegalArgumentException("Username is mandatory."); } if (StringUtils.isEmpty(this.password)) { throw new IllegalArgumentException("Password is mandatory."); } this.deviceId = InstagramHashUtil.generateDeviceId(this.username, this.password); if (StringUtils.isEmpty(this.uuid)) { this.uuid = InstagramGenericUtil.generateUuid(true); } if (this.cookieStore == null) { this.cookieStore = new BasicCookieStore(); } log.info("Device ID is: " + this.deviceId + ", random id: " + this.uuid); this.client = new DefaultHttpClient(); this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); this.client.setCookieStore(this.cookieStore); }
/** * If {@link #reusableHttpClient} is true the instance returned is always the same. * * @param authenticationRequest * the external auth for the currrent request * @return the http client to be used */ private HttpClient getHttpClient(R authenticationRequest) { AbstractHttpClient httpClient = httpClientReusable ? reusableHttpClient : new DefaultHttpClient(); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); if (!httpClientReusable) { configureHttpClient(httpClient, authenticationRequest); } return httpClient; }
/** * HTTP参数配置 * @return */ public BasicHttpParams getHttpParams(){ BasicHttpParams httpParams = new BasicHttpParams(); // 设置每个路由最大连接数 ConnPerRouteBean connPerRoute = new ConnPerRouteBean(30); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute); HttpConnectionParams.setStaleCheckingEnabled(httpParams, false); // 从连接池中取连接的超时时间,设置为1秒 ConnManagerParams.setTimeout(httpParams, mTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(DEFAULT_MAX_CONNECTIONS)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); // 读响应数据的超时时间 HttpConnectionParams.setSoTimeout(httpParams, mTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, mTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams,userAgent); //默认参数 HttpClientParams.setRedirecting(httpParams, false); HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.BROWSER_COMPATIBILITY); httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null); return httpParams; }
/** * 默认构造函数 */ private HttpUtil() { this.charset = IftConf.EnCode; PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(); pccm.setMaxTotal(100); //设置整个连接池最大链接数 httpClient = new DefaultHttpClient(pccm); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY); httpClient.getConnectionManager().closeIdleConnections(30,TimeUnit.SECONDS); }
/** * * @param isMultiRequest * 是否支持多线程 * @param connectionTimeout * 建立连接超时时间(毫秒) * @param socketTimeout * 等待数据超时时间(毫秒) * @return */ public static HttpClient getHttpClient(boolean isMultiRequest, int connectionTimeout, int socketTimeout) { SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault(); ClientConnectionManager cm = isMultiRequest ? new PoolingClientConnectionManager( schemeRegistry) : new BasicClientConnectionManager( schemeRegistry); HttpParams params = isMultiRequest ? new SyncBasicHttpParams() : new BasicHttpParams(); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, socketTimeout); return new DefaultHttpClient(cm, params); }
private void addCookies() { Map<String, List<String>> cookies = httpData.getCookies(); Iterator<String> keys = cookies.keySet().iterator(); String domain = ""; domain = message.getURI().getHost(); CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute( ClientContext.COOKIE_STORE, cookieStore ); client.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY ); while ( keys.hasNext() ) { String nextKey = keys.next(); List<String> values = cookies.get( nextKey ); Date date = new Date( 2038, 1, 1 ); for ( int i = 0; i < values.size(); i++ ) { BasicClientCookie cookie = new BasicClientCookie( nextKey, values.get( i ) ); cookieStore.addCookie( cookie ); cookie.setVersion( 1 ); cookie.setDomain( domain ); cookie.setPath( "/" ); cookie.setExpiryDate( date ); cookie.setSecure( false ); } } client.setCookieStore( cookieStore ); }
private HttpClient getHttpClient() { if (this.httpClient == null) { LOG.log(Level.FINER, "Creating an HttpClient"); BasicHttpParams params = new BasicHttpParams(); params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, this.connectionTimeout) .setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH) .setParameter(AllClientPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8) .setParameter(AllClientPNames.SO_TIMEOUT, this.socketTimeout); this.httpClient = new DefaultHttpClient(params); } return this.httpClient; }
@Override protected Client apacheHttpClientSpecificInitialization() { ApacheHttpClient4 apache = (ApacheHttpClient4) super.apacheHttpClientSpecificInitialization(); apache.getClientHandler().getHttpClient().getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); return apache; }
private HttpParams createClientParams() { HttpParams clientParams = new BasicHttpParams(); clientParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout()); clientParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, getTimeout()); clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // Not sure if this flag has any effect under the new httpcomponents-client code clientParams.setBooleanParameter("http.protocol.single-cookie-header", true); return clientParams; }
/** * Configures the various parameters of the connection manager and the HTTP * client. * * @param params * The parameter list to update. */ protected void configure(HttpParams params) { ConnManagerParams.setMaxTotalConnections(params, getMaxTotalConnections()); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(getMaxConnectionsPerHost())); // Configure other parameters HttpClientParams.setAuthenticating(params, false); HttpClientParams.setRedirecting(params, isFollowRedirects()); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); HttpConnectionParams.setTcpNoDelay(params, getTcpNoDelay()); HttpConnectionParams.setConnectionTimeout(params, getConnectTimeout()); HttpConnectionParams.setSoTimeout(params, getSocketTimeout()); params.setIntParameter(ClientPNames.MAX_REDIRECTS, getMaxRedirects()); //-Dhttp.proxyHost=chlaubc.obs.pmi -Dhttp.proxyPort=8000 -Dhttp.nonProxyHosts=localhost|127.0.0.1|*.app.pmi String httpProxyHost = getProxyHost(); if (httpProxyHost != null) { if (StringUtils.isNotEmpty(getNonProxyHosts())) { System.setProperty("http.nonProxyHosts", getNonProxyHosts()); } else { System.getProperties().remove("http.nonProxyHosts"); } System.setProperty("http.proxyPort", String.valueOf(getProxyPort())); System.setProperty("http.proxyHost", httpProxyHost); HttpHost proxy = new HttpHost(httpProxyHost, getProxyPort()); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } }
public String sendPost(String url, Map<String, String> params, String contentType) { ret = null; try { httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); httpPost = new HttpPost(url); response = null; Log.d(TAG, "Setting httpPost headers"); httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); if (contentType != null) { httpPost.setHeader("Content-Type", contentType); } else { httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); } httpPost.setEntity(buildData(params)); response = httpClient.execute(httpPost, localContext); if (response != null) { ret = EntityUtils.toString(response.getEntity()); } } catch (Throwable e) { Log.e(TAG, "Failed to post to url: " + url, e); e.printStackTrace(); } Log.d(TAG, "Returning value:" + ret); return ret; }
/** * 默认构造函数 */ public HttpUtil() { this.charset = "UTF-8"; httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY); httpClient.getConnectionManager().closeIdleConnections(30,TimeUnit.SECONDS); }
/** * Construct a new HttpClient which uses the {@link #POOL_MGR default connection pool}. * * @param connectionTimeout highly sensitive to application so must be specified * @param socketTimeout highly sensitive to application so must be specified */ public static HttpClient createHttpClient(final int connectionTimeout, final int socketTimeout) { return new DefaultHttpClient(POOL_MGR) {{ HttpParams httpParams = getParams(); // prevent seg fault JVM bug, see https://code.google.com/p/crawler4j/issues/detail?id=136 httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setStaleCheckingEnabled(httpParams, true); }}; }
public DownloadManager(Configuration c) { this.c = c; this.s = new Storage(c); r = new Report(c); u = new URLManager(c.crawlFilter); p = new Parser(c.linkFilter); HttpParams params = new BasicHttpParams(); HttpConnectionParams.setSoTimeout(params, c.responseTimeout * 1000); HttpProtocolParams.setUserAgent(params, c.userAgent); HttpProtocolParams.setContentCharset(params, "UTF-8"); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", c.httpPort, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(c.downloadThreads); cm.setDefaultMaxPerRoute(c.downloadThreads); httpClient = new DefaultHttpClient(cm, params); // TODO: enable compression support (zip, deflate) httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); if (c.useCookies) { httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); } else { httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); } workerService = Executors.newFixedThreadPool(c.downloadThreads); completionService = new ExecutorCompletionService<Resource>(workerService); startTime = System.currentTimeMillis(); }
public HC4CookieHandler(String policy) { super(); if (policy.equals(org.apache.commons.httpclient.cookie.CookiePolicy.DEFAULT)) { // tweak diff HC3 vs HC4 policy = CookiePolicy.BEST_MATCH; } this.cookieSpec = registry.getCookieSpec(policy); }
public static BasicHttpParams defaultParams(int timeout) { BasicHttpParams httpClientParams = new BasicHttpParams(); // connection to server timeouts HttpConnectionParams.setConnectionTimeout(httpClientParams, timeout); HttpConnectionParams.setSoTimeout(httpClientParams, timeout); // set to rfc 2109 as it puts the ASP (IIS) cookie _FIRST_, this is critical for interealty httpClientParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); return httpClientParams; }
private static HttpClient createClient() { ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(), DEFAULT_CONNECTION_MANAGER_TIMEOUT, TimeUnit.MILLISECONDS); DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager); HttpParams clientParams = httpClient.getParams(); HttpConnectionParams.setSocketBufferSize(clientParams, 60000); HttpConnectionParams.setTcpNoDelay(clientParams, false); HttpProtocolParams.setUserAgent(clientParams, VOLDEMORT_USER_AGENT); HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1); // HostConfiguration hostConfig = new HostConfiguration(); // hostConfig.setHost("localhost"); HttpConnectionParams.setConnectionTimeout(clientParams, DEFAULT_CONNECTION_MANAGER_TIMEOUT); HttpConnectionParams.setSoTimeout(clientParams, 500); httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES); connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_HOST_CONNECTIONS); HttpConnectionParams.setStaleCheckingEnabled(clientParams, false); return httpClient; }
public String sendPost(String url, Map<String, String> params, String contentType) { ret = null; try { httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); httpPost = new HttpPost(url); response = null; Log.d(LOG_TAG, "Setting httpPost headers"); httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); if (contentType != null) { httpPost.setHeader("Content-Type", contentType); } else { httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); } httpPost.setEntity(buildData(params)); response = httpClient.execute(httpPost, localContext); if (response != null) { ret = EntityUtils.toString(response.getEntity()); } } catch (Throwable e) { Log.e(LOG_TAG, "Failed to post to url: " + url, e); } Log.d(LOG_TAG, "Returning value:" + ret); return ret; }
/** * Download link and have it be the response. * @param req the http request * @param resp the http response * @param link the link to download * @param c the cookie to set if any * @throws IOException on any error. */ private static void proxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c, String proxyHost) throws IOException { DefaultHttpClient client = new DefaultHttpClient(); client .getParams() .setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY) .setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); // Make sure we send the request from the proxy address in the config // since that is what the AM filter checks against. IP aliasing or // similar could cause issues otherwise. InetAddress localAddress = InetAddress.getByName(proxyHost); if (LOG.isDebugEnabled()) { LOG.debug("local InetAddress for proxy host: {}", localAddress); } client.getParams() .setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress); HttpGet httpGet = new HttpGet(link); @SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames(); while(names.hasMoreElements()) { String name = names.nextElement(); if(passThroughHeaders.contains(name)) { String value = req.getHeader(name); if (LOG.isDebugEnabled()) { LOG.debug("REQ HEADER: {} : {}", name, value); } httpGet.setHeader(name, value); } } String user = req.getRemoteUser(); if (user != null && !user.isEmpty()) { httpGet.setHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII")); } OutputStream out = resp.getOutputStream(); try { HttpResponse httpResp = client.execute(httpGet); resp.setStatus(httpResp.getStatusLine().getStatusCode()); for (Header header : httpResp.getAllHeaders()) { resp.setHeader(header.getName(), header.getValue()); } if (c != null) { resp.addCookie(c); } InputStream in = httpResp.getEntity().getContent(); if (in != null) { IOUtils.copyBytes(in, out, 4096, true); } } finally { httpGet.releaseConnection(); } }
/** * 代理获取内容... * @param servletRequest * @param targetUri * @return * @throws ServletException * @throws IOException * @throws URISyntaxException */ public static String proxy(HttpServletRequest servletRequest, String targetUri) throws Exception { String result = ""; URI targetUriObj = new URI(targetUri); HttpHost targetHost = URIUtils.extractHost(targetUriObj); HttpParams hcParams = new BasicHttpParams(); hcParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); hcParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); // See #70 HttpClient proxyClient = createHttpClient(hcParams); // Make the Request String method = servletRequest.getMethod(); String proxyRequestUri = rewriteUrlFromRequest(servletRequest, targetUri); HttpRequest proxyRequest = new BasicHttpRequest(method, proxyRequestUri); copyRequestHeaders(servletRequest, proxyRequest, targetHost); setXForwardedForHeader(servletRequest, proxyRequest); HttpResponse proxyResponse = null; try { proxyResponse = proxyClient.execute(targetHost, proxyRequest); HttpEntity entity = proxyResponse.getEntity(); if (entity != null) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); entity.writeTo(byteArrayOutputStream); result = byteArrayOutputStream.toString("UTF-8"); // closeQuietly(byteArrayOutputStream); } } finally { // make sure the entire entity was consumed, so the connection is released if (proxyResponse != null){ consumeQuietly(proxyResponse.getEntity()); } } return result; }