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


Java DefaultHttpClientConnection类代码示例

本文整理汇总了Java中org.apache.http.impl.DefaultHttpClientConnection的典型用法代码示例。如果您正苦于以下问题:Java DefaultHttpClientConnection类的具体用法?Java DefaultHttpClientConnection怎么用?Java DefaultHttpClientConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: initHttpClient

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的package包/类
private void initHttpClient() {
    _params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(_params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(_params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(_params, false);
    HttpProtocolParams.setHttpElementCharset(_params, "UTF-8");

    _httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
            // Required protocol interceptors
            new BasicHttpProcessor(),
            new RequestConnControl(),
            new RequestContent(),
            new RequestDate(),
            new RequestTargetHost(),
            // Recommended protocol interceptors
            new RequestUserAgent(),
            new RequestExpectContinue()
    });
    _httpexecutor = new HttpRequestExecutor();
    _httpcontext = new BasicHttpContext(null);
    _connection = new DefaultHttpClientConnection();
    _connectionStrategy = new DefaultConnectionReuseStrategy();
}
 
开发者ID:Juniper,项目名称:contrail-java-api,代码行数:24,代码来源:ApiConnectorImpl.java

示例2: run

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的package包/类
public void run() {
    System.out.println("Listening on port " + this.serversocket.getLocalPort());
    while (!Thread.interrupted()) {
        try {
            // Set up incoming HTTP connection
            Socket insocket = this.serversocket.accept();
            DefaultHttpServerConnection inconn = new DefaultHttpServerConnection();
            System.out.println("Incoming connection from " + insocket.getInetAddress());
            inconn.bind(insocket, this.params);

            // Set up outgoing HTTP connection
            Socket outsocket = new Socket(this.target.getHostName(), this.target.getPort());
            DefaultHttpClientConnection outconn = new DefaultHttpClientConnection();
            outconn.bind(outsocket, this.params);
            System.out.println("Outgoing connection to " + outsocket.getInetAddress());
            
            // Start worker thread
            Thread t = new ProxyThread(this.httpService, inconn, outconn);
            t.setDaemon(true);
            t.start();
        } catch (InterruptedIOException ex) {
            break;
        } catch (IOException e) {
            System.err.println("I/O error initialising connection thread: " 
                    + e.getMessage());
            break;
        }
    }
}
 
开发者ID:mleoking,项目名称:PhET,代码行数:30,代码来源:ElementalReverseProxy.java

示例3: create

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的package包/类
protected HttpClientConnection create(final Socket socket, final HttpParams params) throws IOException {
    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    conn.bind(socket, params);
    return conn;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:6,代码来源:BasicConnFactory.java

示例4: main

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的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

示例5: main

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的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

示例6: sendReport

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的package包/类
public static void sendReport(URL pTarget, String pData) 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(pTarget.getHost(), pTarget.getPort());

        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(pData)};


            for (HttpEntity requestBody : requestBodies) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, params);
                }
                BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
                        pTarget.getPath() + "?" + pTarget.getQuery());

                request.setEntity(requestBody);
                // 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:Torridity,项目名称:dsworkbench,代码行数:64,代码来源:OBSTReportSender.java

示例7: sendPost

import org.apache.http.impl.DefaultHttpClientConnection; //导入依赖的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.impl.DefaultHttpClientConnection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。