/** 执行网络访问 */ private static HttpResult execute(String url, HttpRequestBase requestBase) { boolean isHttps = url.startsWith("https://");//判断是否需要采用https AbstractHttpClient httpClient = HttpClientFactory.create(isHttps); HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext()); HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制 int retryCount = 0; boolean retry = true; while (retry) { try { HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络 if (response != null) { return new HttpResult(response, httpClient, requestBase); } } catch (Exception e) { IOException ioException = new IOException(e.getMessage()); retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事 } } return null; }
private static AbstractHttpClient createHTTPClient() { AbstractHttpClient client = new DefaultHttpClient(); String proxyHost = System.getProperty("https.proxyHost", ""); if (!proxyHost.isEmpty()) { int proxyPort = Integer.parseInt(System.getProperty("https.proxyPort", "-1")); log.info("Using proxy " + proxyHost + ":" + proxyPort); HttpParams params = client.getParams(); HttpHost proxy = new HttpHost(proxyHost, proxyPort); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); String proxyUser = System.getProperty(JMeter.HTTP_PROXY_USER, JMeterUtils.getProperty(JMeter.HTTP_PROXY_USER)); if (proxyUser != null) { log.info("Using authenticated proxy with username: " + proxyUser); String proxyPass = System.getProperty(JMeter.HTTP_PROXY_PASS, JMeterUtils.getProperty(JMeter.HTTP_PROXY_PASS)); String localHost; try { localHost = InetAddress.getLocalHost().getCanonicalHostName(); } catch (Throwable e) { log.error("Failed to get local host name, defaulting to 'localhost'", e); localHost = "localhost"; } AuthScope authscope = new AuthScope(proxyHost, proxyPort); String proxyDomain = JMeterUtils.getPropDefault("http.proxyDomain", ""); NTCredentials credentials = new NTCredentials(proxyUser, proxyPass, localHost, proxyDomain); client.getCredentialsProvider().setCredentials(authscope, credentials); } } return client; }
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) { this.client = client; this.context = context; this.request = request; this.responseHandler = responseHandler; //断点续传处理 if (this.responseHandler instanceof BreakpointHttpResponseHandler) { BreakpointHttpResponseHandler breakpointHandler = (BreakpointHttpResponseHandler)this.responseHandler; File tempFile = breakpointHandler.getTempFile(); if (tempFile.exists()) { long previousFileSize = tempFile.length(); Log.e(tag, "previousFileSized: " + previousFileSize); this.request.setHeader("RANGE", "bytes=" + previousFileSize + "-"); } } }
/** 执行网络访问 */ private static HttpResult execute(String url, HttpRequestBase requestBase) { boolean isHttps = url.startsWith("https://");//判断是否需要采用https AbstractHttpClient httpClient = HttpClientFactory.create(isHttps); HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext()); HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制 int retryCount = 0; boolean retry = true; while (retry) { try { HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络 if (response != null) { return new HttpResult(response, httpClient, requestBase); } } catch (Exception e) { IOException ioException = new IOException(e.getMessage()); retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事 LogUtils.e(e); } } return null; }
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) { this.client = client; this.context = context; this.request = request; this.responseHandler = responseHandler; //断点续传处理 if(this.responseHandler instanceof BreakpointHttpResponseHandler){ BreakpointHttpResponseHandler breakpointHandler = (BreakpointHttpResponseHandler)this.responseHandler; File tempFile = breakpointHandler.getTempFile(); if (tempFile.exists()) { long previousFileSize = tempFile.length(); Log.e(tag, "previousFileSized: " + previousFileSize); this.request.setHeader("RANGE", "bytes=" + previousFileSize + "-"); } } }
/** * This method is used to capture Location headers after HttpClient redirect handling. */ private void setupClient(final AbstractHttpClient client) { this.client.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { Header header = response.getFirstHeader("Location"); if (header!=null) { String location = header.getValue(); /* * Append the base name to the Location header */ if (location.startsWith("/")) { String baseUrl = context.getAttribute(ExecutionContext.HTTP_TARGET_HOST).toString(); location = baseUrl + location; } context.setAttribute("Location", location); } } }); }
/** * Configure credentials and auth scheme on client if an authorization is * available for url * @param client {@link HttpClient} * @param url URL to test * @param credentialsProvider {@link CredentialsProvider} * @param localHost host running JMeter */ public void setupCredentials(HttpClient client, URL url, CredentialsProvider credentialsProvider, String localHost) { Authorization auth = getAuthForURL(url); if (auth != null) { String username = auth.getUser(); String realm = auth.getRealm(); String domain = auth.getDomain(); if (log.isDebugEnabled()){ log.debug(username + " > D="+domain+" R="+realm + " M="+auth.getMechanism()); } if (Mechanism.KERBEROS.equals(auth.getMechanism())) { ((AbstractHttpClient) client).getAuthSchemes().register( AuthSchemes.SPNEGO, new FixedSPNegoSchemeFactory(isStripPort(url))); credentialsProvider.setCredentials(new AuthScope(null, -1, null), USE_JAAS_CREDENTIALS); } else { credentialsProvider.setCredentials( new AuthScope(url.getHost(), url.getPort(), realm.length()==0 ? null : realm), new NTCredentials(username, auth.getPass(), localHost, domain)); } } }
/** * Setup credentials for url AuthScope but keeps Proxy AuthScope credentials * @param client HttpClient * @param url URL * @param authManager {@link AuthManager} * @param key key */ private void setConnectionAuthorization(HttpClient client, URL url, AuthManager authManager, HttpClientKey key) { CredentialsProvider credentialsProvider = ((AbstractHttpClient) client).getCredentialsProvider(); if (authManager != null) { if(authManager.hasAuthForURL(url)) { authManager.setupCredentials(client, url, credentialsProvider, localHost); } else { credentialsProvider.clear(); } } else { Credentials credentials = null; AuthScope authScope = null; if(key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) { authScope = new AuthScope(key.proxyHost, key.proxyPort); credentials = credentialsProvider.getCredentials(authScope); } credentialsProvider.clear(); if(credentials != null) { credentialsProvider.setCredentials(authScope, credentials); } } }
public static AbstractHttpClient createHttpClient() { try { if (IS_HTTP_CLIENT_GE_4_2) { ClientConnectionManager poolingManager = (ClientConnectionManager) Class.forName( "org.apache.http.impl.conn.PoolingClientConnectionManager").newInstance(); ((ConnPoolControl<?>) poolingManager).setMaxTotal(DEFAULT_MAX_TOTAL); ((ConnPoolControl<?>) poolingManager).setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE); return new DefaultHttpClient(poolingManager, (HttpParams) null); } else { return new ThreadSafeHttpClient(); } } catch (Exception e) { throw new IllegalStateException("Can not create http client.", e); } }
@Override protected Client newClient() { ResteasyClientBuilder resteasyClientBuilder = new ResteasyClientBuilder(); ResteasyClient client = resteasyClientBuilder.establishConnectionTimeout(getTimeout(), TimeUnit.MILLISECONDS).socketTimeout(getTimeout(), TimeUnit.MILLISECONDS).build(); AbstractHttpClient httpClient = (AbstractHttpClient) ((ApacheHttpClient4Engine) client.httpEngine()).getHttpClient(); httpClient.setRedirectStrategy(new DefaultRedirectStrategy() { @Override protected boolean isRedirectable(String method) { return true; } }); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { request.setParams(new AllowRedirectHttpParams(request.getParams())); } }); return client; }
/** * 获得cookie * * @param httpClient */ public static void getCookie(HttpClient httpClient) { List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); String cookieName = cookie.getName(); String cookieValue = cookie.getValue(); if (!TextUtils.isEmpty(cookieName) && !TextUtils.isEmpty(cookieValue)) { sb.append(cookieName + "="); sb.append(cookieValue + ";"); } } SharedPreferences.Editor edit = DemoContext.getInstance().getSharedPreferences().edit(); edit.putString("DEMO_COOKIE", sb.toString()); edit.apply(); }
private void setProxySettings (org.apache.http.client.HttpClient client, HasProxySettings proxySettings, String prot) { if (client == null) return ; if (proxySettings == null || !proxySettings.isActive()) return ; if (prot == null || prot.isEmpty()) return ; org.apache.http.HttpHost proxy = new org.apache.http.HttpHost(proxySettings.getHost(), proxySettings.getPort(), prot) ; client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy) ; CredentialsProvider credProvider = ((AbstractHttpClient) client).getCredentialsProvider(); credProvider.setCredentials( new AuthScope(proxySettings.getHost(), proxySettings.getPort()), new UsernamePasswordCredentials(proxySettings.getUsername(), proxySettings.getPassword())); }
/** * Returns the HttpClient singleton. */ public static synchronized HttpClient getHttpClient() { if (httpClient == null) { if (AppConfig.DEBUG) Log.d(TAG, "Creating new instance of HTTP client"); HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, AppPreferences.USER_AGENT); params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS); params.setBooleanParameter("http.protocol.reject-relative-redirect", false); HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpClientParams.setRedirecting(params, true); httpClient = new DefaultHttpClient(createClientConnectionManager(), params); // Workaround for broken URLs in redirection ((AbstractHttpClient) httpClient) .setRedirectHandler(new APRedirectHandler()); } return httpClient; }
@Override public HttpClient get() { AbstractHttpClient client = new DefaultHttpClient() { @Override protected ClientConnectionManager createClientConnectionManager() { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", getHttpsSocketFactory(), 443)); final HttpParams params = getParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT); HttpProtocolParams.setUserAgent(params, getUserAgentString(HttpProtocolParams.getUserAgent(params))); return new ThreadSafeClientConnManager(params, registry); } }; return client; }
/** * * This function adds support for pre-emptive HTTP Authentication for an HttpClient. * * @param httpClient */ public static void makeAuthenticationPreemptive(HttpClient httpClient) { HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request,final HttpContext context) throws HttpException,IOException{ AuthState authState = (AuthState) context .getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context .getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }; ((AbstractHttpClient) httpClient).addRequestInterceptor(preemptiveAuth,0); }
public static AbstractHttpClient createNonverifyingHttpClient() throws Exception { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new NonverifyingSSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); }
public synchronized AbstractHttpClient create(String userAgent) { if (userAgent == null) { userAgent = ""; } // From Apache HttpClient doc: "HttpClient is fully thread-safe when used // with a thread-safe connection manager." The // PoolingClientConnectionManager we are using is such as thread-safe one. AbstractHttpClient client = new RedirectHttpClient(connectionManager); prepareHttpClient(client); if (userAgent.length() > 0) { client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent); } return client; }
private static SimplHttpResponse doRequest(String userAgent, final String url, HttpUriRequest request) throws IOException { AbstractHttpClient client = factory.create(userAgent); // client must use BasicRedirectStrategy! final BasicRedirectStrategy redirectStrategy = (BasicRedirectStrategy) client.getRedirectStrategy(); SimplHttpResponse result = client.execute(request, new ResponseHandler<SimplHttpResponse>() { @Override public SimplHttpResponse handleResponse(HttpResponse response) throws ClientProtocolException, IOException { List<String> redirectedLocations = redirectStrategy.getRedirectedLocations(); return SimplHttpResponse.parse(url, redirectedLocations, response); } }); return result; }
@Factory("solrServer") public void connectToSolrServer() { log.info("Connecting to solr server"); String url = "http://localhost:8080/solr/" + platform; solrServer = null; solrServer = new HttpSolrServer(url); HttpSolrServer server = (HttpSolrServer) solrServer; if (solrUsername != null && solrPassword != null) ((AbstractHttpClient)server.getHttpClient()).addRequestInterceptor(new PreEmptiveBasicAuthenticator(solrUsername, solrPassword)); server.setSoTimeout(1000); // socket read timeout server.setConnectionTimeout(100); server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); // server.setFollowRedirects(false); // defaults to false // allowCompression defaults to false. // Server side must support gzip or deflate for this to have any effect. server.setAllowCompression(true); server.setMaxRetries(1); // defaults to 0. > 1 not recommended. // server.setParser(new XMLResponseParser()); // binary parser is used by default }
public static void enableAuth(final AbstractHttpClient client, final Keychain keychain, final KeyId keyId) { if (client == null) { throw new NullPointerException("client"); } if (keychain == null) { throw new NullPointerException("keychain"); } client.getAuthSchemes().register(Constants.SCHEME, new AuthSchemeFactory() { public AuthScheme newInstance(HttpParams params) { return new Http4SignatureAuthScheme(); } }); Signer signer = new Signer(keychain, keyId); client.getCredentialsProvider().setCredentials(AuthScope.ANY, new SignerCredentials(signer)); client.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(Constants.SCHEME)); HttpClientParams.setAuthenticating(client.getParams(), true); }
/** * Uses the HttpClient provided instead of creating a new one. This lets the CopyJob reuse the * HttpClient (which caches the connections) for an entire Job. This is dirty, because not all * adapters have an HTTPClient. * * @param profile * @param sslExceptionCallback * @param httpClient * @throws StorageAdapterException * if we cannot initialize */ public Hcp3AuthNamespaceAdapter(Hcp3AuthNamespaceProfile profile, SSLCertificateCallback sslExceptionCallback, AbstractHttpClient httpClient) throws StorageAdapterException { super(); this.profile = profile; this.httpClient = httpClient; if (httpClient == null) { init(sslExceptionCallback); } this.sslExceptionCallback = sslExceptionCallback; debugName = "hcap3AuthNamespaceAdapter" + ++adapterCnt; }
/** * Uses the HttpClient provided instead of creating a new one. This lets the CopyJob reuse the * HttpClient (which caches the connections) for an entire Job. This is dirty, because not all * adapters have an HTTPClient. * * @param profile * @param sslExceptionCallback * @param httpClient * @throws StorageAdapterException * if we cannot initialize */ public Hcap2Adapter(Hcap2Profile profile, SSLCertificateCallback sslExceptionCallback, AbstractHttpClient httpClient) throws StorageAdapterException { super(); this.profile = profile; this.httpClient = httpClient; if (httpClient == null) { init(sslExceptionCallback); } this.sslExceptionCallback = sslExceptionCallback; // debug start debugName = "hcp2adapter" + ++adapterCnt; }
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, AsyncHttpResponseHandler responseHandler) { this.client = client; this.context = context; this.request = request; this.responseHandler = responseHandler; if (responseHandler instanceof BinaryHttpResponseHandler) { this.isBinaryRequest = true; } }
private String getCaptchaId(HttpClient httpClient) { List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies(); String captchaId = null; for (int i = 0; i < cookies.size(); i++) { Cookie cookie = (Cookie) cookies.get(i); String cookieName = cookie.getName(); if (!TextUtils.isEmpty(cookieName) && cookieName.equals("captchaId")) { captchaId = cookie.getValue(); } } return captchaId; }
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) { this.client = (AbstractHttpClient) Utils.notNull(client, "client"); this.context = (HttpContext) Utils.notNull(context, "context"); this.request = (HttpUriRequest) Utils.notNull(request, SocialConstants.TYPE_REQUEST); this.responseHandler = (ResponseHandlerInterface) Utils.notNull(responseHandler, "responseHandler"); }
public HttpHandler(AbstractHttpClient client, HttpContext context, String charset, RequestCallBack<T> callback) { this.client = client; this.context = context; this.callback = callback; this.charset = charset; this.client.setRedirectHandler(notUseApacheRedirectHandler); }
/** * 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; }
/** * 执行网络访问 */ private static void execute(String url, HttpRequestBase requestBase, HttpCallbackListener httpCallbackListener) { boolean isHttps = url.startsWith("https://");//判断是否需要采用https AbstractHttpClient httpClient = HttpClientFactory.create(isHttps); HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext()); HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制 int retryCount = 0; boolean retry = true; while (retry) { try { HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络 int stateCode = response.getStatusLine().getStatusCode(); // LogUtils.e(TAG, "http状态码:" + stateCode); if (response != null) { if (stateCode == HttpURLConnection.HTTP_OK){ HttpResult httpResult = new HttpResult(response, httpClient, requestBase); String result = httpResult.getString(); if (!TextUtils.isEmpty(result)){ httpCallbackListener.onSuccess(result); return; } else { throw new RuntimeException("数据为空"); } } else { throw new RuntimeException(HttpRequestCode.ReturnCode(stateCode)); } } } catch (Exception e) { IOException ioException = new IOException(e.getMessage()); retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事 LogUtils.e(TAG, "重复次数:" + retryCount + " :"+ e); if (!retry){ httpCallbackListener.onError(e); } } } }
/** * */ private void closeThreadLocalConnections() { // Does not need to be synchronised, as all access is from same thread Map<HttpClientKey, HttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get(); if ( mapHttpClientPerHttpClientKey != null ) { for ( HttpClient cl : mapHttpClientPerHttpClientKey.values() ) { ((AbstractHttpClient) cl).clearRequestInterceptors(); ((AbstractHttpClient) cl).clearResponseInterceptors(); ((AbstractHttpClient) cl).close(); cl.getConnectionManager().shutdown(); } mapHttpClientPerHttpClientKey.clear(); } }
private Set<String> getRequestInterceptors(final AbstractHttpClient abstractHttpClient) { final LinkedHashSet<String> requestInterceptors = new LinkedHashSet<>(); for (int ii = 0; ii < abstractHttpClient.getRequestInterceptorCount(); ii++) { requestInterceptors.add(abstractHttpClient.getRequestInterceptor(ii).getClass().getName()); } return requestInterceptors; }
private Set<String> getResponseInterceptors(final AbstractHttpClient abstractHttpClient) { final LinkedHashSet<String> responseInterceptors = new LinkedHashSet<>(); for (int ii = 0; ii < abstractHttpClient.getResponseInterceptorCount(); ii++) { responseInterceptors.add(abstractHttpClient.getResponseInterceptor(ii).getClass().getName()); } return responseInterceptors; }
public CacheAsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, CacheTextResponseHandler responseHandler, boolean isCacheAble, String cacheKey, File cacheDir, int cacheTime) { super(client, context, request, responseHandler); this.client = client; this.context = context; this.request = request; this.responseHandler = responseHandler; // this.isCacheAble = isCacheAble; this.cacheDir = cacheDir; this.cacheTime = cacheTime; this.cacheKey = TextUtils.isEmpty(cacheKey) ? request.getURI().toString() : cacheKey; }