java Apache HttpComponents: org.apache.http.client.ClientProtocolException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11347635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache HttpComponents: org.apache.http.client.ClientProtocolException
提问by Nactive
So I use apache HttpComponents to handle http request in java.
Now I want to reuse the DefaultHttpClient
, what should be possible accoarding to this example: http://wiki.apache.org/HttpComponents/QuickStart. The example itselfs give a ssl error so I modefied and simplefied it a bit. Now I always get a org.apache.http.client.ClientProtocolException
所以我使用 apache HttpComponents 来处理 java 中的 http 请求。现在我想重用DefaultHttpClient
,根据这个例子应该是什么:http://wiki.apache.org/HttpComponents/QuickStart。该示例本身给出了 ssl 错误,因此我对其进行了修改和简化。现在我总是得到一个org.apache.http.client.ClientProtocolException
Here is my example program, basicly I just request 2 webpages using the same DefaultHttpClient
.
这是我的示例程序,基本上我只是使用相同的DefaultHttpClient
.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class ClientFormLogin {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
//Handle first request.
HttpGet httpget = new HttpGet("http://tweakers.net/nieuws/82969/amazon-nederland-opent-digitale-deuren-in-september.html");
HttpResponse response = httpclient.execute(httpget);
System.out.println("Execute finished");
HttpEntity entity = response.getEntity();
String page = readInput(entity.getContent());
System.out.println("Request one finished without problems!");
//Handle second request
HttpGet httpost = new HttpGet("http://gathering.tweakers.net/forum/list_messages/1506977/last");
response = httpclient.execute(httpost);
entity = response.getEntity();
page = readInput(entity.getContent());
System.out.println("Request two finished without problems!");
}
private static String readInput(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte bytes[] = new byte[1024];
int n = in.read(bytes);
while (n != -1) {
out.write(bytes, 0, n);
n = in.read(bytes);
}
return new String(out.toString());
}
}
When runnig my example I get the folowing error
运行我的示例时,出现以下错误
Request one finished without problems!
Exception in thread "main" org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at ClientFormLogin.main(ClientFormLogin.java:29)
Caused by: org.apache.http.HttpException: Unable to establish route: planned = {}->http://gathering.tweakers.net; current = {}->http://tweakers.net
at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:842)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
... 3 more
Any one can give me some pointers how I can solve this problem, except use a new DefaultHttpClient
for every request.
任何人都可以给我一些指示我如何解决这个问题,除了DefaultHttpClient
对每个请求使用一个新的。
Edit
编辑
I just found out if I stay on the same domain I have no problems so:
我刚刚发现如果我留在同一个域中,我没有问题,所以:
page1: 'http://tweakers.net/nieuws/82969/amazon-nederland-opent-digitale-deuren-in-september.html'
page2: 'http://tweakers.net/nieuws/82973/website-nujij-belandt-op-zwarte-lijst-google-door-malware.html'
I have no problems if I got to:
如果我必须这样做,我没有问题:
page1: 'http://tweakers.net/nieuws/82969/amazon-nederland-opent-digitale-deuren-in-september.html'
page2: 'http://gathering.tweakers.net/forum/list_messages/1506076/last'
I get the error.
我得到了错误。
Ofc I see this one minute after posting my question. Except if someone can tell me how I can go 2 sperate domains with the same DefaultHttpClient
my question is already answered.
Ofc 我在发布我的问题后一分钟看到了这个。除非有人可以告诉我如何使用相同的 2 个单独的域,否则DefaultHttpClient
我的问题已经得到了回答。
回答by kaliatech
This is likely due to a recent bug in the v4.2 BasicClientConnectionManager affecting cross site redirects. See http://issues.apache.org/jira/browse/HTTPCLIENT-1193.
这可能是由于 v4.2 BasicClientConnectionManager 中最近的一个错误影响了跨站点重定向。请参阅http://issues.apache.org/jira/browse/HTTPCLIENT-1193。
According to the maintainer, one temporary workaround is to use SingleClientConnManager or PoolingClientConnectionManager. Perhaps something like this:
根据维护者的说法,一种临时解决方法是使用 SingleClientConnManager 或 PoolingClientConnectionManager。也许是这样的:
ClientConnectionManager connManager = new PoolingClientConnectionManager();
DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
...