常见的HTTPclient和代理 [英] Common HTTPclient and proxy

查看:169
本文介绍了常见的HTTPclient和代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用apache的常见httpclient库。是否有可能通过代理发出HTTP请求?更具体地说,我需要使用多线程POST请求的代理列表(现在我正在测试单线程GET请求)。

  httpclient.getHostConfiguration()。setProxy(67.177.104.230,58720); 

我得到该错误代码:

  21.03.2012。 20时49分17秒org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 
产品介绍:连接被拒绝:连接
21.03.2012处理请求时抓住了I / O异常(java.net.ConnectException)。 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO:重试请求
21.03.2012。 20时49分19秒org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
产品介绍:连接被拒绝:连接
21.03.2012处理请求时抓住了I / O异常(java.net.ConnectException)。 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO:重试请求
21.03.2012。 20时49分21秒org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
产品介绍:连接被拒绝:连接
21.03.2012处理请求时抓住了I / O异常(java.net.ConnectException)。 20时49分21秒org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
产品介绍:重试请求
org.apache.commons.httpclient.ProtocolException:服务器XXXXX无法以有效的HTTP响应$ B回应$ b。在org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1846)在org.apache.commons.httpclient.HttpMethodBase.readResponse
(HttpMethodBase.java:1590)
。在组织.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)在org.apache.commons.httpclient.ConnectMethod.execute(ConnectMethod.java:144)

在org.apache.commons .httpClient.HttpMethodDirector.executeConnect(HttpMethodDirector.java:495)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:390)
at org.apache.commons.httpclient.HttpMethodDirector (HttpClient.java:396)
。在org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at test.main(test.java:42)

当我删除该行时,所有事情都按预期运行良好。 对于httpclient 4.1.x,你可以像这样设置代理(取自

.0.1,8080,http);

DefaultHttpClient httpclient = new DefaultHttpClient();
尝试{
httpclient.getParams()。setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpHost target = new HttpHost(issues.apache.org,443,https);
HttpGet req = new HttpGet(/);

System.out.println(通过+ proxy执行请求到+ target +;
HttpResponse rsp = httpclient.execute(target,req);
...
} finally {
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
httpclient.getConnectionManager()。shutdown();
}


I am using apache's common httpclient library. Is it possible to make HTTP request over proxy? More specific, I need to use proxy list for multithreaded POST requests (right now I am testing with single threaded GET requests).

I tried to use:

        httpclient.getHostConfiguration().setProxy("67.177.104.230", 58720);

I get errors with that code:

21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
org.apache.commons.httpclient.ProtocolException: The server xxxxx failed to respond with a valid HTTP response
    at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1846)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
    at org.apache.commons.httpclient.ConnectMethod.execute(ConnectMethod.java:144)
    at org.apache.commons.httpclient.HttpMethodDirector.executeConnect(HttpMethodDirector.java:495)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:390)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at test.main(test.java:42)

When I remove that line, everything runs fine as expected.

解决方案

For httpclient 4.1.x you can set the proxy like this (taken from this example):

    HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        HttpHost target = new HttpHost("issues.apache.org", 443, "https");
        HttpGet req = new HttpGet("/");

        System.out.println("executing request to " + target + " via " + proxy);
        HttpResponse rsp = httpclient.execute(target, req);
        ...
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

这篇关于常见的HTTPclient和代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆