当前位置: 首页>>代码示例>>Java>>正文


Java HttpRequestExecutor.preProcess方法代码示例

本文整理汇总了Java中org.apache.http.protocol.HttpRequestExecutor.preProcess方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestExecutor.preProcess方法的具体用法?Java HttpRequestExecutor.preProcess怎么用?Java HttpRequestExecutor.preProcess使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.protocol.HttpRequestExecutor的用法示例。


在下文中一共展示了HttpRequestExecutor.preProcess方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: execute

import org.apache.http.protocol.HttpRequestExecutor; //导入方法依赖的package包/类
public HttpResponse execute(HttpRequest request) throws IOException, HttpException {
  HttpParams params = new BasicHttpParams();
  HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

  HttpProcessor processor = new ImmutableHttpProcessor(new RequestContent());

  HttpRequestExecutor executor = new HttpRequestExecutor();

  HttpContext context = new BasicHttpContext(null);
  context.setAttribute(ExecutionContext.HTTP_CONNECTION, connection);

  if (!connection.isOpen()) {
    Socket socket = new Socket(address.getAddress(), address.getPort());
    connection.bind(socket, params);
  }

  context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
  request.setParams(params);
  executor.preProcess(request, processor, context);
  HttpResponse response = executor.execute(request, connection, context);
  executor.postProcess(response, processor, context);

  return response;
}
 
开发者ID:ApptuitAI,项目名称:JInsight,代码行数:25,代码来源:RequestExecutorBasedClientInstrumentationTest.java

示例2: main

import org.apache.http.protocol.HttpRequestExecutor; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    
    HttpParams params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
            // Required protocol interceptors
            new RequestContent(),
            new RequestTargetHost(),
            // Recommended protocol interceptors
            new RequestConnControl(),
            new RequestUserAgent(),
            new RequestExpectContinue()});
    
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    
    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost("localhost", 8080);

    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

    try {
        
        String[] targets = {
                "/",
                "/servlets-examples/servlet/RequestInfoExample", 
                "/somewhere%20in%20pampa"};
        
        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]);
            System.out.println(">> Request URI: " + request.getRequestLine().getUri());
            
            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());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("==============");
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else {
                System.out.println("Connection kept alive...");
            }
        }
    } finally {
        conn.close();
    }
}
 
开发者ID:mleoking,项目名称:PhET,代码行数:63,代码来源:ElementalHttpGet.java

示例3: main

import org.apache.http.protocol.HttpRequestExecutor; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    
    HttpParams params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);
    
    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
            // Required protocol interceptors
            new RequestContent(),
            new RequestTargetHost(),
            // Recommended protocol interceptors
            new RequestConnControl(),
            new RequestUserAgent(),
            new RequestExpectContinue()});
    
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

    HttpContext context = new BasicHttpContext(null);
    
    HttpHost host = new HttpHost("localhost", 8080);
    
    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

    try {
        
        HttpEntity[] requestBodies = {
                new StringEntity(
                        "This is the first test request", "UTF-8"),
                new ByteArrayEntity(
                        "This is the second test request".getBytes("UTF-8")),
                new InputStreamEntity(
                        new ByteArrayInputStream(
                                "This is the third test request (will be chunked)"
                                .getBytes("UTF-8")), -1)
        };
        
        for (int i = 0; i < requestBodies.length; i++) {
            if (!conn.isOpen()) {
                Socket socket = new Socket(host.getHostName(), host.getPort());
                conn.bind(socket, params);
            }
            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", 
                    "/servlets-examples/servlet/RequestInfoExample");
            request.setEntity(requestBodies[i]);
            System.out.println(">> Request URI: " + request.getRequestLine().getUri());

            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());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("==============");
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else {
                System.out.println("Connection kept alive...");
            }
        }
    } finally {
        conn.close();
    }        
}
 
开发者ID:mleoking,项目名称:PhET,代码行数:72,代码来源:ElementalHttpPost.java

示例4: testReleaseConnectionOnAbort

import org.apache.http.protocol.HttpRequestExecutor; //导入方法依赖的package包/类
/**
 * Tests releasing connection from #abort method called from the
 * main execution thread while there is no blocking I/O operation.
 */
@Test
public void testReleaseConnectionOnAbort() throws Exception {

    this.connManager.setMaxTotal(1);

    final HttpHost target = start();
    final HttpRoute route = new HttpRoute(target, null, false);
    final int      rsplen = 8;
    final String      uri = "/random/" + rsplen;
    final HttpContext context = new BasicHttpContext();

    final HttpRequest request =
        new BasicHttpRequest("GET", uri, HttpVersion.HTTP_1_1);

    HttpClientConnection conn = getConnection(this.connManager, route);
    this.connManager.connect(conn, route, 0, context);
    this.connManager.routeComplete(conn, route, context);

    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
    context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);

    final HttpProcessor httpProcessor = new ImmutableHttpProcessor(
            new HttpRequestInterceptor[] { new RequestContent(), new RequestConnControl() });

    final HttpRequestExecutor exec = new HttpRequestExecutor();
    exec.preProcess(request, httpProcessor, context);
    final HttpResponse response = exec.execute(request, conn, context);

    Assert.assertEquals("wrong status in first response",
                 HttpStatus.SC_OK,
                 response.getStatusLine().getStatusCode());

    // check that there are no connections available
    try {
        // this should fail quickly, connection has not been released
        getConnection(this.connManager, route, 100L, TimeUnit.MILLISECONDS);
        Assert.fail("ConnectionPoolTimeoutException should have been thrown");
    } catch (final ConnectionPoolTimeoutException e) {
        // expected
    }

    // abort the connection
    Assert.assertTrue(conn instanceof HttpClientConnection);
    conn.shutdown();
    this.connManager.releaseConnection(conn, null, -1, null);

    // the connection is expected to be released back to the manager
    conn = getConnection(this.connManager, route, 5L, TimeUnit.SECONDS);
    Assert.assertFalse("connection should have been closed", conn.isOpen());

    this.connManager.releaseConnection(conn, null, -1, null);
    this.connManager.shutdown();
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:58,代码来源:TestConnectionManagement.java

示例5: sendPost

import org.apache.http.protocol.HttpRequestExecutor; //导入方法依赖的package包/类
public static Element sendPost(ConnectorManager connectorManager, String servletPath, Document document) {
		try {
			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, "UTF-8");
			HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
			HttpProtocolParams.setUseExpectContinue(params, true);

			BasicHttpProcessor httpproc = new BasicHttpProcessor();
			// Required protocol interceptors
			httpproc.addInterceptor(new RequestContent());
			httpproc.addInterceptor(new RequestTargetHost());
			// Recommended protocol interceptors
			httpproc.addInterceptor(new RequestConnControl());
			httpproc.addInterceptor(new RequestUserAgent());
			httpproc.addInterceptor(new RequestExpectContinue());

			HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

			HttpContext context = new BasicHttpContext(null);

			URL connectorManagerURL = new URL(connectorManager.getUrl());
			HttpHost host = new HttpHost(connectorManagerURL.getHost(), connectorManagerURL.getPort());

			DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
			ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

			context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
			context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

			try {
				HttpEntity requestBody;
				if (document != null) {
//			        OutputFormat format = OutputFormat.createPrettyPrint();
				    OutputFormat format = OutputFormat.createCompactFormat();
                    StringWriter stringWriter = new StringWriter();
			        XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
			        xmlWriter.write(document);
			        String xmlAsString = stringWriter.toString();
					requestBody = new StringEntity(xmlAsString, "UTF-8");
				} else {
					requestBody = null;
				}
				if (!conn.isOpen()) {
					Socket socket = new Socket(host.getHostName(), host.getPort());
					conn.bind(socket, params);
				}

				String target = connectorManager.getUrl() + servletPath;

				BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", target);
				request.setEntity(requestBody);
				LOGGER.info(">> Request URI: " + request.getRequestLine().getUri());

				request.setParams(params);
				httpexecutor.preProcess(request, httpproc, context);
				HttpResponse response = httpexecutor.execute(request, conn, context);
				response.setParams(params);
				httpexecutor.postProcess(response, httpproc, context);

				LOGGER.info("<< Response: " + response.getStatusLine());
				String entityText = EntityUtils.toString(response.getEntity());
				LOGGER.info(entityText);
				LOGGER.info("==============");
				if (!connStrategy.keepAlive(response, context)) {
					conn.close();
				} else {
					LOGGER.info("Connection kept alive...");
				}

				Document xml = DocumentHelper.parseText(entityText);
				return xml.getRootElement();
			} finally {
				conn.close();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
 
开发者ID:BassJel,项目名称:Jouve-Project,代码行数:80,代码来源:ConnectorManagerRequestUtils.java


注:本文中的org.apache.http.protocol.HttpRequestExecutor.preProcess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。