繁体   English   中英

Apache HttpComponents:org.apache.http.client.ClientProtocolException

[英]Apache HttpComponents: org.apache.http.client.ClientProtocolException

因此,我使用apache HttpComponents处理Java中的http请求。 现在,我想重用DefaultHttpClient ,此示例应该可以做到这一点: http ://wiki.apache.org/HttpComponents/QuickStart。 该示例本身给出了一个ssl错误,因此我对其进行了简化和简化。 现在我总是得到一个org.apache.http.client.ClientProtocolException

这是我的示例程序,基本上我只使用相同的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());
    }
}

在运行我的示例时,出现以下错误

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

除了为每个请求使用新的DefaultHttpClient之外,任何人都可以为我提供一些如何解决此问题的指示。


编辑

我只是发现如果我留在同一个域中就没有问题,所以:

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'

如果我要:

page1: 'http://tweakers.net/nieuws/82969/amazon-nederland-opent-digitale-deuren-in-september.html'
page2: 'http://gathering.tweakers.net/forum/list_messages/1506076/last'

我得到了错误。

我在发布问题后一分钟就看到了Ofc。 除非有人可以告诉我如何使用相同的DefaultHttpClient进入2个单独的域,否则我的问题已得到解答。

这可能是由于v4.2 BasicClientConnectionManager中的最新错误影响了跨站点重定向。 参见http://issues.apache.org/jira/browse/HTTPCLIENT-1193

根据维护者的说法,一种临时的解决方法是使用SingleClientConnManager或PoolingClientConnectionManager。 也许是这样的:

ClientConnectionManager connManager = new PoolingClientConnectionManager();
DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM