org.apache.http.protocol.HttpRequestExecutor Java Examples
The following examples show how to use
org.apache.http.protocol.HttpRequestExecutor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockHttpClient.java From google-http-java-client with Apache License 2.0 | 6 votes |
@Override protected RequestDirector createClientRequestDirector( HttpRequestExecutor requestExec, ClientConnectionManager conman, ConnectionReuseStrategy reustrat, ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor, HttpRequestRetryHandler retryHandler, RedirectHandler redirectHandler, AuthenticationHandler targetAuthHandler, AuthenticationHandler proxyAuthHandler, UserTokenHandler stateHandler, HttpParams params) { return new RequestDirector() { @Beta public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException, IOException { return new BasicHttpResponse(HttpVersion.HTTP_1_1, responseCode, null); } }; }
Example #2
Source File: ApacheHttpClient.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private ConnectionManagerAwareHttpClient createClient(ApacheHttpClient.DefaultBuilder configuration, AttributeMap standardOptions) { ApacheConnectionManagerFactory cmFactory = new ApacheConnectionManagerFactory(); HttpClientBuilder builder = HttpClients.custom(); // Note that it is important we register the original connection manager with the // IdleConnectionReaper as it's required for the successful deregistration of managers // from the reaper. See https://github.com/aws/aws-sdk-java/issues/722. HttpClientConnectionManager cm = cmFactory.create(configuration, standardOptions); builder.setRequestExecutor(new HttpRequestExecutor()) // SDK handles decompression .disableContentCompression() .setKeepAliveStrategy(buildKeepAliveStrategy(standardOptions)) .disableRedirectHandling() .disableAutomaticRetries() .setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time .setConnectionManager(ClientConnectionManagerFactory.wrap(cm)); addProxyConfig(builder, configuration); if (useIdleConnectionReaper(standardOptions)) { IdleConnectionReaper.getInstance().registerConnectionManager( cm, standardOptions.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis()); } return new ApacheSdkHttpClient(builder.build(), cm); }
Example #3
Source File: HttpClientIT.java From pinpoint with Apache License 2.0 | 5 votes |
@Test public void test() throws Exception { HttpClient httpClient = new DefaultHttpClient(); try { HttpPost post = new HttpPost(webServer.getCallHttpUrl()); post.addHeader("Content-Type", "application/json;charset=UTF-8"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); httpClient.execute(post, responseHandler); } catch (Exception ignored) { } finally { if (null != httpClient && null != httpClient.getConnectionManager()) { httpClient.getConnectionManager().shutdown(); } } PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); Class<?> connectorClass; try { connectorClass = Class.forName("org.apache.http.impl.conn.ManagedClientConnectionImpl"); } catch (ClassNotFoundException e) { connectorClass = Class.forName("org.apache.http.impl.conn.AbstractPooledConnAdapter"); } verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", AbstractHttpClient.class.getMethod("execute", HttpUriRequest.class, ResponseHandler.class))); final String hostname = webServer.getHostAndPort(); verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", connectorClass.getMethod("open", HttpRoute.class, HttpContext.class, HttpParams.class), annotation("http.internal.display", hostname))); verifier.verifyTrace(event("HTTP_CLIENT_4", HttpRequestExecutor.class.getMethod("execute", HttpRequest.class, HttpClientConnection.class, HttpContext.class), null, null, hostname, annotation("http.url", "/"), annotation("http.status.code", 200), annotation("http.io", anyAnnotationValue()))); verifier.verifyTraceCount(0); }
Example #4
Source File: LibHttpClient.java From YiBo with Apache License 2.0 | 5 votes |
@Override protected RequestDirector createClientRequestDirector( final HttpRequestExecutor requestExec, final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler, final RedirectHandler redirectHandler, final AuthenticationHandler targetAuthHandler, final AuthenticationHandler proxyAuthHandler, final UserTokenHandler stateHandler, final HttpParams params) { return new LibRequestDirector( requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectHandler, targetAuthHandler, proxyAuthHandler, stateHandler, params); }
Example #5
Source File: TestTwitterSocket.java From albert with MIT License | 4 votes |
public static void main(String[] args) throws Exception { OAuthConsumer consumer = new CommonsHttpOAuthConsumer( Constants.ConsumerKey, Constants.ConsumerSecret); consumer.setTokenWithSecret(Constants.AccessToken, Constants.AccessSecret); HttpParams params = new BasicHttpParams(); // HTTP 协议的版本,1.1/1.0/0.9 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 字符集 HttpProtocolParams.setContentCharset(params, "UTF-8"); // 伪装的浏览器类型 // IE7 是 // Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0) // // Firefox3.03 // Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 // HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); HttpProtocolParams.setUseExpectContinue(params, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); HttpHost host = new HttpHost("127.0.0.1", 1080); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); try { String[] targets = { "https://www.twitter.com"}; for (int i = 0; i < targets.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, params); } BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]); // consumer.sign(request); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); context.setAttribute(ExecutionContext.HTTP_REQUEST, request); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); // 返回码 System.out.println("<< Response: " + response.getStatusLine()); // 返回的文件头信息 // Header[] hs = response.getAllHeaders(); // for (Header h : hs) { // System.out.println(h.getName() + ":" + h.getValue()); // } // 输出主体信息 // System.out.println(EntityUtils.toString(response.getEntity())); HttpEntity entry = response.getEntity(); StringBuffer sb = new StringBuffer(); if(entry != null) { InputStreamReader is = new InputStreamReader(entry.getContent()); BufferedReader br = new BufferedReader(is); String str = null; while((str = br.readLine()) != null) { sb.append(str.trim()); } br.close(); } System.out.println(sb.toString()); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
Example #6
Source File: MicrometerHttpRequestExecutorTest.java From micrometer with Apache License 2.0 | 4 votes |
private HttpClient client(HttpRequestExecutor executor) { return HttpClientBuilder.create() .setRequestExecutor(executor) .build(); }
Example #7
Source File: MicrometerHttpRequestExecutorTest.java From micrometer with Apache License 2.0 | 4 votes |
private HttpRequestExecutor executor(boolean exportRoutes) { return MicrometerHttpRequestExecutor.builder(registry) .exportTagsForRoute(exportRoutes) .build(); }
Example #8
Source File: HttpClientUtil.java From lucene-solr with Apache License 2.0 | 4 votes |
public static CloseableHttpClient createClient(final SolrParams params, PoolingHttpClientConnectionManager cm, boolean sharedConnectionManager, HttpRequestExecutor httpRequestExecutor) { final ModifiableSolrParams config = new ModifiableSolrParams(params); if (log.isDebugEnabled()) { log.debug("Creating new http client, config: {}", config); } cm.setMaxTotal(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS, 10000)); cm.setDefaultMaxPerRoute(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 10000)); cm.setValidateAfterInactivity(Integer.getInteger(VALIDATE_AFTER_INACTIVITY, VALIDATE_AFTER_INACTIVITY_DEFAULT)); HttpClientBuilder newHttpClientBuilder = HttpClientBuilder.create(); if (sharedConnectionManager) { newHttpClientBuilder.setConnectionManagerShared(true); } else { newHttpClientBuilder.setConnectionManagerShared(false); } ConnectionKeepAliveStrategy keepAliveStrat = new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // we only close connections based on idle time, not ttl expiration return -1; } }; if (httpClientBuilder.getAuthSchemeRegistryProvider() != null) { newHttpClientBuilder.setDefaultAuthSchemeRegistry(httpClientBuilder.getAuthSchemeRegistryProvider().getAuthSchemeRegistry()); } if (httpClientBuilder.getCookieSpecRegistryProvider() != null) { newHttpClientBuilder.setDefaultCookieSpecRegistry(httpClientBuilder.getCookieSpecRegistryProvider().getCookieSpecRegistry()); } if (httpClientBuilder.getCredentialsProviderProvider() != null) { newHttpClientBuilder.setDefaultCredentialsProvider(httpClientBuilder.getCredentialsProviderProvider().getCredentialsProvider()); } newHttpClientBuilder.addInterceptorLast(new DynamicInterceptor()); newHttpClientBuilder = newHttpClientBuilder.setKeepAliveStrategy(keepAliveStrat) .evictIdleConnections((long) Integer.getInteger(EVICT_IDLE_CONNECTIONS, EVICT_IDLE_CONNECTIONS_DEFAULT), TimeUnit.MILLISECONDS); if (httpRequestExecutor != null) { newHttpClientBuilder.setRequestExecutor(httpRequestExecutor); } HttpClientBuilder builder = setupBuilder(newHttpClientBuilder, params); HttpClient httpClient = builder.setConnectionManager(cm).build(); assert ObjectReleaseTracker.track(httpClient); return (CloseableHttpClient) httpClient; }
Example #9
Source File: CloaeableHttpClientIT.java From pinpoint with Apache License 2.0 | 4 votes |
@Test public void test() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet(webServer.getCallHttpUrl()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { instream.read(); } catch (IOException ex) { throw ex; } finally { instream.close(); } } } finally { response.close(); } } finally { httpclient.close(); } PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", CloseableHttpClient.class.getMethod("execute", HttpUriRequest.class))); final String display = webServer.getHostAndPort(); verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", PoolingHttpClientConnectionManager.class.getMethod("connect", HttpClientConnection.class, HttpRoute.class, int.class, HttpContext.class), annotation("http.internal.display", display))); final String destinationId = webServer.getHostAndPort(); verifier.verifyTrace(event("HTTP_CLIENT_4", HttpRequestExecutor.class.getMethod("execute", HttpRequest.class, HttpClientConnection.class, HttpContext.class), null, null, destinationId, annotation("http.url", "/"), annotation("http.status.code", 200), annotation("http.io", anyAnnotationValue()))); verifier.verifyTraceCount(0); }
Example #10
Source File: LibRequestDirector.java From YiBo with Apache License 2.0 | 4 votes |
public LibRequestDirector( final HttpRequestExecutor requestExec, final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler, final RedirectHandler redirectHandler, final AuthenticationHandler targetAuthHandler, final AuthenticationHandler proxyAuthHandler, final UserTokenHandler userTokenHandler, final HttpParams params) { if (requestExec == null) { throw new IllegalArgumentException("Request executor may not be null."); } if (conman == null) { throw new IllegalArgumentException("Client connection manager may not be null."); } if (reustrat == null) { throw new IllegalArgumentException("Connection reuse strategy may not be null."); } if (kastrat == null) { throw new IllegalArgumentException("Connection keep alive strategy may not be null."); } if (rouplan == null) { throw new IllegalArgumentException("Route planner may not be null."); } if (httpProcessor == null) { throw new IllegalArgumentException("HTTP protocol processor may not be null."); } if (retryHandler == null) { throw new IllegalArgumentException("HTTP request retry handler may not be null."); } if (redirectHandler == null) { throw new IllegalArgumentException("Redirect handler may not be null."); } if (targetAuthHandler == null) { throw new IllegalArgumentException("Target authentication handler may not be null."); } if (proxyAuthHandler == null) { throw new IllegalArgumentException("Proxy authentication handler may not be null."); } if (userTokenHandler == null) { throw new IllegalArgumentException("User token handler may not be null."); } if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } this.requestExec = requestExec; this.connManager = conman; this.reuseStrategy = reustrat; this.keepAliveStrategy = kastrat; this.routePlanner = rouplan; this.httpProcessor = httpProcessor; this.retryHandler = retryHandler; this.redirectHandler = redirectHandler; this.targetAuthHandler = targetAuthHandler; this.proxyAuthHandler = proxyAuthHandler; this.userTokenHandler = userTokenHandler; this.params = params; this.managedConn = null; this.execCount = 0; this.redirectCount = 0; this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100); this.targetAuthState = new AuthState(); this.proxyAuthState = new AuthState(); }