/** * Configures the httpClient to connect to the URL provided. * * @param authToken */ private static DefaultHttpClient getHttpClient() { DefaultHttpClient httpClient = new DefaultHttpClient(); final HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, HTTP_REQUEST_TIMEOUT_MS); HttpConnectionParams.setSoTimeout(params, HTTP_REQUEST_TIMEOUT_MS); ConnManagerParams.setTimeout(params, HTTP_REQUEST_TIMEOUT_MS); HttpClientParams.setRedirecting(params, false); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); httpClient.addRequestInterceptor(new DumpHeadersRequestInterceptor()); httpClient.addResponseInterceptor(new DumpHeadersResponseInterceptor()); httpClient.addResponseInterceptor(new CookieQuotesFixerResponseInterceptor()); return httpClient; }
/** * new {@link DefaultHttpClient}, and set strategy. * * @return DefaultHttpClient */ private DefaultHttpClient createApacheHttpClient(BasicHttpParams httpParams) { DefaultHttpClient httpClient = new DefaultHttpClient(createClientConnManager(httpParams), httpParams); // disable apache default redirect handler httpClient.setRedirectHandler( new RedirectHandler() { @Override public boolean isRedirectRequested(HttpResponse response, HttpContext context) { return false; } @Override public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException { return null; } }); // disable apache default retry handler httpClient.setHttpRequestRetryHandler( new HttpRequestRetryHandler() { @Override public boolean retryRequest( IOException exception, int executionCount, HttpContext context) { return false; } }); // enable gzip supporting in request httpClient.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(org.apache.http.HttpRequest request, HttpContext context) { if (!request.containsHeader(Consts.HEADER_ACCEPT_ENCODING)) { request.addHeader(Consts.HEADER_ACCEPT_ENCODING, Consts.ENCODING_GZIP); } } }); // enable gzip supporting in response httpClient.addResponseInterceptor( new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(Consts.ENCODING_GZIP)) { response.setEntity(new GZIPEntityWrapper(entity)); break; } } } } }); // setKeepAlive(httpClient); return httpClient; }
public HttpUtils(int connTimeout, String userAgent) { HttpParams params = new BasicHttpParams(); ConnManagerParams.setTimeout(params, connTimeout); HttpConnectionParams.setSoTimeout(params, connTimeout); HttpConnectionParams.setConnectionTimeout(params, connTimeout); if (TextUtils.isEmpty(userAgent)) { userAgent = OtherUtils.getUserAgent(null); } HttpProtocolParams.setUserAgent(params, userAgent); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(10)); ConnManagerParams.setMaxTotalConnections(params, 10); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSocketBufferSize(params, 1024 * 8); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", DefaultSSLSocketFactory.getSocketFactory(), 443)); httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_RETRY_TIMES)); httpClient.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(org.apache.http.HttpRequest httpRequest, HttpContext httpContext) throws org.apache.http.HttpException, IOException { if (!httpRequest.containsHeader(HEADER_ACCEPT_ENCODING)) { httpRequest.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } } }); httpClient.addResponseInterceptor( new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext httpContext) throws org.apache.http.HttpException, IOException { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GZipDecompressingEntity(response.getEntity())); return; } } } } }); }
private HttpClient newHttpClient(String sslProtocol, HttpRequest request) { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, httpTimeoutsProvider.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(params, httpTimeoutsProvider.getSocketTimeout()); // AG-1059: Need to follow redirects to ensure that if the host app is setup with Apache and // mod_jk we can still // fetch internal gadgets. if (request.getFollowRedirects()) { params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setIntParameter(ClientPNames.MAX_REDIRECTS, 3); } params.setParameter( ClientPNames.DEFAULT_HEADERS, ImmutableSet.of(new BasicHeader("Accept-Encoding", "gzip, deflate"))); DefaultHttpClient client = new DefaultHttpClient(params); // AG-1044: Need to use the JVM's default proxy configuration for requests. ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); client.setRoutePlanner(routePlanner); client.addResponseInterceptor(new GzipDeflatingInterceptor()); client .getConnectionManager() .getSchemeRegistry() .register(new Scheme("https", new CustomSSLSocketFactory(sslProtocol), 443)); return client; }
/** Creates a new AsyncHttpClient. */ public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent( httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor( new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor( new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }
public Http(Context context) { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, 10); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor( new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor( new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); clientHeaderMap = new HashMap<String, String>(); }
/** * 获得没有任何设置的实例 * * @return */ public static HttpClient getCleanHttpClient() { DefaultHttpClient httpClient = new DefaultHttpClient(getHttpParams()); // httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, // new HttpHost("localhost", 8888)); httpClient.addRequestInterceptor(getRequestInter()); httpClient.addResponseInterceptor(getResponseInter()); httpClient.setHttpRequestRetryHandler(buildMyRetryHandler()); return httpClient; }
/* (non-Javadoc) * @see java.net.URLConnection#connect() */ @Override public void connect() throws IOException { if (m_client != null) { return; } final HttpParams httpParams = new BasicHttpParams(); if (m_request != null) { int timeout = m_request.getParameterAsInt("timeout"); if (timeout > 0) { HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setSoTimeout(httpParams, timeout); } } m_client = new DefaultHttpClient(httpParams); m_client.addRequestInterceptor(new RequestAcceptEncoding()); m_client.addResponseInterceptor(new ResponseContentEncoding()); if (m_request != null) { int retries = m_request.getParameterAsInt("retries"); if (retries > 0) { m_client.setHttpRequestRetryHandler( new DefaultHttpRequestRetryHandler() { @Override public boolean retryRequest( IOException exception, int executionCount, HttpContext context) { if (executionCount <= getRetryCount() && (exception instanceof SocketTimeoutException || exception instanceof ConnectTimeoutException)) { return true; } return super.retryRequest(exception, executionCount, context); } }); } String disableSslVerification = m_request.getParameter("disable-ssl-verification"); if (Boolean.parseBoolean(disableSslVerification)) { final SchemeRegistry registry = m_client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); try { SSLSocketFactory factory = new SSLSocketFactory( SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } catch (NoSuchAlgorithmException e) { LOG.warn(e.getMessage()); } } } }
static { httpClient.addResponseInterceptor( new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { if (response.containsHeader("Location")) { Header[] locations = response.getHeaders("Location"); if (locations.length > 0) context.setAttribute(LAST_REDIRECT_URL, locations[0].getValue()); } } }); }
/** * 获得连接池的实例 * * @return */ public static HttpClient getCoonPoolHttpClient() { PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); cm.setMaxTotal(800); cm.setDefaultMaxPerRoute(200); DefaultHttpClient httpClient = new DefaultHttpClient(cm, getHttpParams()); httpClient.addRequestInterceptor(getRequestInter()); httpClient.addResponseInterceptor(getResponseInter()); httpClient.setHttpRequestRetryHandler(buildMyRetryHandler()); return httpClient; }
private static HttpClient setupHttpClient(boolean gzip) { SSLSocketFactory.getSocketFactory() .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); BasicHttpParams params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(20)); params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 200); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); DefaultHttpClient httpClient = new DefaultHttpClient(cm, new BasicHttpParams()); httpClient.setHttpRequestRetryHandler( new DefaultHttpRequestRetryHandler(HTTP_RETRY_COUNT, true)); if (gzip) { httpClient.addRequestInterceptor( new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); httpClient.addResponseInterceptor( new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); } return httpClient; }
public HttpClient getAuthorizedClient(String hostName, int port) { DefaultHttpClient client = new DefaultHttpClient(); client.addRequestInterceptor(new ShopifyRequestInterceptor()); client.addResponseInterceptor( new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { /* CRest 1.0.1 throws an exception is the status code != 200, * even on 201 Created, just translate all 200 responses to error code 200. */ int code = response.getStatusLine().getStatusCode(); if (code >= 200 && code < 300) { response.setStatusCode(200); } } }); AuthScope scope = new AuthScope(hostName, port); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(credential.getApiKey(), credential.getPassword()); client.getCredentialsProvider().setCredentials(scope, creds); return client; }
/* (non-Javadoc) * @see java.net.URLConnection#connect() */ @Override public void connect() throws IOException { if (m_client != null) { return; } m_client = new DefaultHttpClient(); m_client.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); m_client.addResponseInterceptor( new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } } }); }
private CacheManager(final Context context) { if (!isAlreadyInitialized.compareAndSet(false, true)) { throw new RuntimeException("Attempt to initialize the cache twice."); } this.context = context; dbManager = new CacheDbManager(context); RequestHandlerThread requestHandler = new RequestHandlerThread(); // TODo put somewhere else -- make request specific, no restart needed on prefs change! final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter( CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 2); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50); params.setParameter( ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 25; } }); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry); final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connManager, params); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); defaultHttpClient.addResponseInterceptor( new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final Header encHeader = entity.getContentEncoding(); if (encHeader == null) return; for (final HeaderElement elem : encHeader.getElements()) { if ("gzip".equalsIgnoreCase(elem.getName())) { response.setEntity(new GzipDecompressingEntity(entity)); return; } } } }); downloadQueue = new PrioritisedDownloadQueue(defaultHttpClient); requestHandler.start(); for (int i = 0; i < 5; i++) { // TODO remove constant --- customizable final CacheDownloadThread downloadThread = new CacheDownloadThread(downloadQueue, true); downloadThreads.add(downloadThread); } }
/** * 构造器,进行client的参数设置,包括Header、Cookie等 * * @param aconfig * @param cookies */ public void init(SpiderConfig config, Site _site) { this.config = config; // 设置HTTP参数 HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString()); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout()); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout()); HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params); paramsBean.setVersion(HttpVersion.HTTP_1_1); paramsBean.setContentCharset("UTF-8"); paramsBean.setUseExpectContinue(false); 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 ThreadSafeClientConnManager(schemeRegistry); connectionManager.setMaxTotal(config.getMaxTotalConnections()); connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); httpClient = new DefaultHttpClient(connectionManager, params); httpClient.getParams().setIntParameter("http.socket.timeout", 60000); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, config.isFollowRedirects()); // HttpClientParams.setCookiePolicy(httpClient.getParams(),CookiePolicy.BEST_MATCH); // 设置响应拦截器 httpClient.addResponseInterceptor( new HttpResponseInterceptor() { 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) { // 处理GZIP解压缩 if (codec.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); if (_site != null) { this.site = _site; if (this.site.getHeaders() != null && this.site.getHeaders().getHeader() != null) { for (org.eweb4j.spiderman.xml.Header header : this.site.getHeaders().getHeader()) { this.addHeader(header.getName(), header.getValue()); } } if (this.site.getCookies() != null && this.site.getCookies().getCookie() != null) { for (org.eweb4j.spiderman.xml.Cookie cookie : this.site.getCookies().getCookie()) { this.addCookie(cookie.getName(), cookie.getValue(), cookie.getHost(), cookie.getPath()); } } } }
/** @return {@link DefaultHttpClient} instance. */ private HttpClient createHttpClient(CouchDbProperties props) { DefaultHttpClient httpclient = null; try { SchemeSocketFactory ssf = null; if (props.getProtocol().equals("https")) { TrustManager trustManager = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return null; } }; SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] {trustManager}, null); ssf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SSLSocket socket = (SSLSocket) ssf.createSocket(null); socket.setEnabledCipherSuites(new String[] {"SSL_RSA_WITH_RC4_128_MD5"}); } else { ssf = PlainSocketFactory.getSocketFactory(); } SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme(props.getProtocol(), props.getPort(), ssf)); PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry); httpclient = new DefaultHttpClient(ccm); host = new HttpHost(props.getHost(), props.getPort(), props.getProtocol()); context = new BasicHttpContext(); // Http params httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); httpclient .getParams() .setParameter(CoreConnectionPNames.SO_TIMEOUT, props.getSocketTimeout()); httpclient .getParams() .setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, props.getConnectionTimeout()); int maxConnections = props.getMaxConnections(); if (maxConnections != 0) { ccm.setMaxTotal(maxConnections); ccm.setDefaultMaxPerRoute(maxConnections); } if (props.getProxyHost() != null) { HttpHost proxy = new HttpHost(props.getProxyHost(), props.getProxyPort()); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } // basic authentication if (props.getUsername() != null && props.getPassword() != null) { httpclient .getCredentialsProvider() .setCredentials( new AuthScope(props.getHost(), props.getPort()), new UsernamePasswordCredentials(props.getUsername(), props.getPassword())); props.clearPassword(); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } // request interceptor httpclient.addRequestInterceptor( new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { if (log.isInfoEnabled()) { RequestLine requestLine = request.getRequestLine(); try { log.info( ">> " + (new StringBuilder()) .append(">> ") .append(requestLine.getMethod()) .append(" ") .append(urlCodec.decode(requestLine.getUri())) .toString()); } catch (DecoderException e) { log.error(e, e); } } } }); // response interceptor httpclient.addResponseInterceptor( new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws IOException { validate(response); if (log.isInfoEnabled()) log.info("<< Status: " + response.getStatusLine().getStatusCode()); } }); } catch (Exception e) { log.error("Error Creating HTTP client. " + e.getMessage()); throw new IllegalStateException(e); } return httpclient; }
public static void main(String[] args) { DefaultHttpClient client = (DefaultHttpClient) HttpClientFactory.createHttpClient(); int day = 1; String time = "dinner"; while (true) { try { client.addResponseInterceptor( new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } } }); HttpPost post = new HttpPost("https://disneyworld.disney.go.com/activities/dining-availability"); post.setHeader("Accept", "*/*"); post.setHeader("Accept-Encoding", "gzip,deflate,sdch"); post.setHeader("Accept-Language", "en-US,en;q=0.8"); post.setHeader("Connection", "keep-alive"); // post.setHeader("Content-Length", "178"); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setHeader( "Cookie", "optimizelyEndUserId=oeu1358558437847r0.07084921281784773; __qca=P0-661515119-1358558438978; dolWA30=1362951732650; grvinsights=ee419b7b50b4f796e86088c3b1d2ad42; cto_firstUrl=\"http://disneyrewards.com/perks/disneyland-perks\"; cto_visitorId=1377887977144-9354867790880; cto_firstPageName=\"dcore:drv:perks:disneyland-perks\"; cto_sessionCount=1; cto_firstRefUrl=na; ctoTimeStamp=1387999595863; backplane-id=41231508470D4B238A7306575348FC41; bp_channel_id=https%3A%2F%2Fapi.echoenabled.com%2Fv1%2Fbus%2Fespn.go.com%2Fchannel%2F41231508470D4B238A7306575348FC41; fbm_116656161708917=base_domain=.go.com; rampenableCheckoutGuestPoc_A=%7B%22enableCheckoutGuestPoc%22%3Atrue%7D; analytics_targus=074%3B2978363885011DB0%7C6000010F800014AC; s_fid=52C9B90BC5607FB9-09F500455ED40B17; gi=usa|mo|kansas city|cable|39.009|-94.402|64118|f02fc4f4; rememberme=%7B%22name%22%3A%22Greg%22%2C%22lastName%22%3A%22Meyer%22%2C%22avatar%22%3A%2217532228%22%2C%22swid%22%3A%22%7BACA6E4C3-4397-43EF-A4AD-259926923E09%7D%22%2C%22passiveId%22%3A%2259f894315e2a19e7402e67fe4c6ff747da4e010fe12261eae24d14c73be8a8c57ccad22feb6201c1d8c4dde74475121d59bb112697d7f5753362f6c5eff25a68%22%2C%22email%22%3A%22kmeyer%40kc.rr.com%22%7D; SWID=ACA6E4C3-4397-43EF-A4AD-259926923E09; GEOLOCATION_jar=%7B%22region%22%3A%22missouri%22%2C%22country%22%3A%22united+states%22%7D; roomForm_jar=%7B%22checkInDate%22%3A%222014-09-01%22%2C%22checkOutDate%22%3A%222014-09-08%22%2C%22numberOfAdults%22%3A%222%22%2C%22numberOfChildren%22%3A%221%22%2C%22accessible%22%3A%220%22%2C%22resort%22%3A%2280010394%3BentityType%3Dresort%22%2C%22kid1%22%3A%2216%22%7D; currentOffer_jar=%7B%22currentOffer%22%3A%22resort-stay-dining%22%7D; 55170107-VID=53301077421770; optimizelySegments=%7B%22166049822%22%3A%22none%22%2C%22166646996%22%3A%22false%22%2C%22167330480%22%3A%22search%22%2C%22167351530%22%3A%22gc%22%2C%22173942781%22%3A%22gc%22%2C%22174819705%22%3A%22false%22%2C%22175220085%22%3A%22search%22%2C%22175226102%22%3A%22referral%22%2C%22175226103%22%3A%22none%22%2C%22175370703%22%3A%22false%22%2C%22175404369%22%3A%22none%22%2C%22175412291%22%3A%22gc%22%2C%22310954789%22%3A%22none%22%2C%22311043393%22%3A%22false%22%2C%22311047346%22%3A%22gc%22%2C%22311052307%22%3A%22referral%22%2C%22793783561%22%3A%22true%22%2C%22806111078%22%3A%22none%22%2C%22806950111%22%3A%22desktop%22%7D; optimizelyBuckets=%7B%7D; CRBLM=CBLM-001:AAAAAAABBGwAAAAB; CRBLM_LAST_UPDATE=1400616530:ACA6E4C3-4397-43EF-A4AD-259926923E09; PHPSESSID=oq7t0rqmdj9j4lb99b97r33vq3; CART-wdw_jar=%7B%22cartId%22%3A%22124935657330-9131021-6107974-2755370%22%7D; mbox=level#20#1403208529|PC#1391624422331-680398.17_31#1408406023|traffic#false#1407237700|check#true#1400630083|session#1400630022656-797538#1400631883; wdpro_seen_cmps=/55541/%2C/55542/%2C/56321/%2C/53949/; pep_oauth_token=BpZ7TgrYTjMmmNfOJcYmNA; boomr_rt=cl=1400630030984&nu=https%3A%2F%2Fdisneyworld.disney.go.com%2Fdining%2Fmagic-kingdom%2Fbe-our-guest-restaurant%2F%23; localeCookie_jar=%7B%22contentLocale%22%3A%22en_US%22%2C%22precedence%22%3A0%2C%22version%22%3A%221%22%7D; WDPROView=%7B%22version%22%3A2%2C%22preferred%22%3A%7B%22device%22%3A%22desktop%22%2C%22screenWidth%22%3A250%2C%22screenHeight%22%3A150%2C%22screenDensity%22%3A1%7D%2C%22deviceInfo%22%3A%7B%22device%22%3A%22desktop%22%2C%22screenWidth%22%3A250%2C%22screenHeight%22%3A150%2C%22screenDensity%22%3A1%7D%7D; s_vi=[CS]v1|2978363885011DB0-6000010F800014AC[CE]; s_pers=%20s_cpm%3D%255B%255B'SOC-DPFY14Q2EpcotInternationalFood'%252C'1392899303875'%255D%252C%255B'SOC-DPFY14Q2EpcotInternationalFood'%252C'1392899304121'%255D%252C%255B'SOC-DPFY14Q2EpcotInternationalFood'%252C'1392899304679'%255D%252C%255B'SOC-DPFY14Q2EpcotInternationalFood'%252C'1392899310315'%255D%252C%255B'SOC-DPFY14Q2EpcotInternationalFood'%252C'1392899314146'%255D%255D%7C1550665714146%3B%20s_c20%3D1397045509015%7C1491653509015%3B%20s_c20_s%3DMore%2520than%25207%2520days%7C1397047309015%3B%20s_c24%3D1400616538424%7C1495224538424%3B%20s_c24_s%3DLess%2520than%25201%2520day%7C1400618338424%3B%20s_gpv_pn%3Dwdpro%252Fwdw%252Fus%252Fen%252Ftools%252Ffinder%252Fdining%252Fmagickingdom%252Fbeourguestrestaurant%7C1400631862919%3B; s_sess=%20s_cc%3Dtrue%3B%20prevPageLoadTime%3Dwdpro%252Fwdw%252Fus%252Fen%252Ftools%252Ffinder%252Fdining%252Fmagickingdom%252Fbeourguestrestaurant%257C5.4%3B%20s_ppv%3D-%252C48%252C45%252C1167%3B%20s_wdpro_lid%3D%3B%20s_sq%3D%3B"); post.setHeader("Host", "disneyworld.disney.go.com"); post.setHeader("Origin", "https://disneyworld.disney.go.com"); post.setHeader( "Referer", "https://disneyworld.disney.go.com/dining/magic-kingdom/be-our-guest-restaurant/"); post.setHeader( "User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"); post.setHeader("X-Requested-With", "XMLHttpRequest"); String content = getRequestContent(day, time); final ByteArrayEntity entity = new ByteArrayEntity(content.getBytes()); entity.setContentType(MediaType.APPLICATION_JSON); entity.setContentEncoding("UTF-8"); post.setEntity(entity); HttpResponse response = client.execute(post); HttpEntity responseEntity = response.getEntity(); String responseContent = new String(IOUtils.toByteArray(responseEntity.getContent())); System.out.print("Availability for Sept " + day + " at " + time + "\r\n\r\n\r\n"); System.out.println(responseContent); System.out.print("\r\n\r\n-----------------------------------------------\r\n\r\n"); if (!responseContent.contains("No tables")) System.exit(0); if (time.equalsIgnoreCase("dinner")) time = "4"; else { time = "dinner"; ++day; if (day > 8) day = 1; } Thread.currentThread().sleep(500); responseEntity.getContent().close(); } catch (Exception e) { // e.printStackTrace(); } } }
private void addGzipInterceptors(DefaultHttpClient httpclient) { httpclient.addRequestInterceptor(new GzipRequestInterceptor()); httpclient.addResponseInterceptor(new GzipResponseInterceptor()); }
/** * {@inheritDoc} <br> * <br> * The default implementation does all of this and more, including using a connection pool and * killing connections after a timeout to use less battery power on mobile devices. It's unlikely * that you'll want to change this behavior. */ @Override public synchronized HttpClient getHttpClient() { if (client == null) { // Set up default connection params. There are two routes to // Dropbox - api server and content server. HttpParams connParams = new BasicHttpParams(); ConnManagerParams.setMaxConnectionsPerRoute( connParams, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute route) { return 10; } }); ConnManagerParams.setMaxTotalConnections(connParams, 20); // Set up scheme registry. SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); DBClientConnManager cm = new DBClientConnManager(connParams, schemeRegistry); try { SSLContext sslContext = SSLContext.getInstance("SSL"); 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()); SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme("https", 443, sf); schemeRegistry.register(httpsScheme); } catch (Exception e) { e.printStackTrace(); } schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); // Set up client params. HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, DEFAULT_TIMEOUT_MILLIS); HttpConnectionParams.setSoTimeout(httpParams, DEFAULT_TIMEOUT_MILLIS); HttpConnectionParams.setSocketBufferSize(httpParams, 8192); HttpProtocolParams.setUserAgent( httpParams, "OfficialDropboxJavaSDK/" + DropboxAPI.SDK_VERSION); DefaultHttpClient c = new DefaultHttpClient(cm, httpParams) { @Override protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { return new DBKeepAliveStrategy(); } @Override protected ConnectionReuseStrategy createConnectionReuseStrategy() { return new DBConnectionReuseStrategy(); } }; c.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); c.addResponseInterceptor( new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } } }); client = c; } return client; }
/** * Creates a new fetcher for fetching HTTP objects. Not really suitable for production use. Use of * an HTTP proxy for security is also necessary for production deployment. * * @param maxObjSize Maximum size, in bytes, of the object we will fetch, 0 if no limit.. * @param connectionTimeoutMs timeout, in milliseconds, for connecting to hosts. * @param readTimeoutMs timeout, in millseconds, for unresponsive connections * @param basicHttpFetcherProxy The http proxy to use. */ public BasicHttpFetcher( int maxObjSize, int connectionTimeoutMs, int readTimeoutMs, String basicHttpFetcherProxy) { // Create and initialize HTTP parameters setMaxObjectSizeBytes(maxObjSize); setSlowResponseWarning(DEFAULT_SLOW_RESPONSE_WARNING); HttpParams params = new BasicHttpParams(); ConnManagerParams.setTimeout(params, connectionTimeoutMs); // These are probably overkill for most sites. ConnManagerParams.setMaxTotalConnections(params, 1152); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(256)); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(params, "Apache Shindig"); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMs); HttpConnectionParams.setSoTimeout(params, readTimeoutMs); HttpConnectionParams.setStaleCheckingEnabled(params, true); HttpClientParams.setRedirecting(params, true); HttpClientParams.setAuthenticating(params, false); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // UCSF. Change to allow self signed registered certs /** * try { SSLContext 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; } * * <p>public void checkClientTrusted(X509Certificate[] certs, String authType) { * System.out.println("checkClientTrusted ============="); } * * <p>public void checkServerTrusted(X509Certificate[] certs, String authType) { * System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); * SSLSocketFactory sf = new SSLSocketFactory(sslContext); * sf.setHostnameVerifier((X509HostnameVerifier)org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); * schemeRegistry.register(new Scheme("https", sf, 443)); } catch (Exception e) { throw new * RuntimeException(e); } */ schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); // end UCSF ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); DefaultHttpClient client = new DefaultHttpClient(cm, params); // Set proxy if set via guice. if (!StringUtils.isEmpty(basicHttpFetcherProxy)) { String[] splits = basicHttpFetcherProxy.split(":"); ConnRouteParams.setDefaultProxy( client.getParams(), new HttpHost(splits[0], Integer.parseInt(splits[1]), "http")); } // try resending the request once client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true)); // Add hooks for gzip/deflate client.addRequestInterceptor( new HttpRequestInterceptor() { public void process(final org.apache.http.HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip, deflate"); } } }); client.addResponseInterceptor( new HttpResponseInterceptor() { public void process( final org.apache.http.HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { for (HeaderElement codec : ceheader.getElements()) { String codecname = codec.getName(); if ("gzip".equalsIgnoreCase(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } } } } } }); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler()); // Disable automatic storage and sending of cookies (see SHINDIG-1382) client.removeRequestInterceptorByClass(RequestAddCookies.class); client.removeResponseInterceptorByClass(ResponseProcessCookies.class); // Use Java's built-in proxy logic in case no proxy set via guice. if (StringUtils.isEmpty(basicHttpFetcherProxy)) { ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); client.setRoutePlanner(routePlanner); } FETCHER = client; }
/** * Creates a new AsyncHttpClient and set timeout. * * @param timeout 超时时间 */ public AsyncHttpClient(int timeout) { this.socketTimeout = timeout; BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent( httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); SSLSocketFactory sf = null; try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); sf = new MySSLSocketFactory(trustStore); } catch (Exception e) { e.printStackTrace(); } sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", sf, 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor( new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor( new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); /* HttpHost proxy = NetworkUtils.getHttpHostProxy(); if (proxy!=null) { // set proxy httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } */ threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }