dar*_*cro 22 java proxy post get httpclient
我正在使用apache的常见httpclient库.是否可以通过代理发出HTTP请求?更具体地说,我需要使用代理列表来处理多线程POST请求(现在我正在使用单线程GET请求进行测试).
我试着用:
httpclient.getHostConfiguration().setProxy("67.177.104.230", 58720);
Run Code Online (Sandbox Code Playgroud)
我得到了该代码的错误:
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)
Run Code Online (Sandbox Code Playgroud)
当我删除该行时,一切都按预期正常运行.
Kai*_*nad 34
对于httpclient 4.1.x,您可以像这样设置代理(取自此示例):
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();
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*gue 12
以下是如何使用HTTPClient
(4.3.4)的最新版本执行此操作
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpHost target = new HttpHost("localhost", 443, "https");
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
HttpGet request = new HttpGet("/");
request.setConfig(config);
System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy);
CloseableHttpResponse response = httpclient.execute(target, request);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
Run Code Online (Sandbox Code Playgroud)
Apache的HTTPComponents版本4.3.x HttpClientBuilder类开始设定从系统属性中的代理默认http.proxyHost
和http.proxyPort
否则,你可以使用setProxy方法覆盖它们.
小智 7
虽然这个问题很老,但我看到的还是没有确切的答案.我会在这里试着回答这个问题.
我相信这里的问题是如何设置Apache commons HttpClient(org.apache.commons.httpclient.HttpClient)的代理设置.
下面的代码段应该有效:
HttpClient client = new HttpClient();
HostConfiguration hostConfiguration = client.getHostConfiguration();
hostConfiguration.setProxy("localhost", 8080);
client.setHostConfiguration(hostConfiguration);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
103412 次 |
最近记录: |