java 使用 Apache DefaultHttpClient 访问受密码保护的 URL 时,什么可能导致 CircularRedirectException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6697502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:56:33  来源:igfitidea点击:

What might cause a CircularRedirectException when accessing a password-protected URL with Apache DefaultHttpClient?

javaapache-httpclient-4.x

提问by arsenal

I am trying to access a page that requires authentication. So I passed my username and password in the code. And this is the below output and error I am getting. First of all it execute the request http://me.somehost.com/and I get the error as Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to. But afterwards it tries to access http://me.somehost.com/robots.txtand I get the response back from the server since it authenticates my username and password. The response I am getting is the actual response If I type my username and password in to the browser with that link.. Then why it is happening with this link http://me.somehost.com/

我正在尝试访问需要身份验证的页面。所以我在代码中传递了我的用户名和密码。这是我得到的以下输出和错误。首先它执行请求http://me.somehost.com/,我得到错误为Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to. 但是之后它尝试访问http://me.somehost.com/robots.txt并且我从服务器得到响应,因为它验证了我的用户名和密码。我得到的响应是实际响应 如果我使用该链接在浏览器中输入我的用户名和密码.. 那么为什么会出现这个链接http://me.somehost.com/

----------------------------------------
executing requestGET http://qhome.somehost.com/ HTTP/1.1
org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
    at edu.uci.ics.crawler4j.url.WebURL.setURL(WebURL.java:113)
    at edu.uci.ics.crawler4j.crawler.CrawlController.addSeed(CrawlController.java:207)
    at edu.uci.ics.crawler4j.example.advanced.Controller.main(Controller.java:31)
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://me.somehost.com/net/pages/Home.xhtml'
    at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:168)
    at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:193)
    at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:1021)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:482)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    ... 5 more
----------------------------------------
executing requestGET http://me.somehost.com/robots.txt HTTP/1.1
HTTP/1.1 200 OK
# TS Application Portfolio: http://cm.somehost.com/cm/
# TS Email ID: [email protected]
# ITOC Qwiki TS Apps Section:  http://ki.somehost.com/itall/ITOC_Esion#QBAT-TS
User-agent: *
Disallow: /departments/
Disallow: /Mnet/pages/
Disallow: /Mnet/themes/
Disallow: /wps/
 INFO [main] Number of pages fetched per second: 0
----------------------------------------
executing requestGET https://login.somehost.com/siteminderagent/64219/smgetcred.scc?TYPE=16777217&REALM=-SM-somehost%202B7NS3b0k0Fk&TARGET=-SM-http%3a%2f%2fqhome%2esomehost%2ecom%2frobots%2etxt HTTP/1.1
HTTP/1.1 200 OK
# TS Application Portfolio: http://cm.somehost.com/cm/
# TS Email ID: [email protected]
# ITOC wiki TS Apps Section:  http://ki.somehost.com/itall/ITOC_Escalation#QBAT-TS
User-agent: *
Disallow: /departments/
Disallow: /net/pages/
Disallow: /net/themes/
Disallow: /wps/

And this is my authentication code..

这是我的身份验证代码..

DefaultHttpClient client = null;

        try
        {
            // Set url
            //URI uri = new URI(url.toString());

            client = new DefaultHttpClient();

            client.getCredentialsProvider().setCredentials(
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                    new UsernamePasswordCredentials("test", "test"));

            // Set timeout
            //client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

            URL url1 = new URL (url);
            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
            connection.setFollowRedirects(true);
            HttpGet request = new HttpGet(url);

            System.out.println("----------------------------------------");
            System.out.println("executing request" + request.getRequestLine());
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();


            System.out.println(response.getStatusLine());





                    InputStream content = entity.getContent();
                    BufferedReader in   = 
                        new BufferedReader (new InputStreamReader (content));
                    String line;
                    while ((line = in.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch(Exception e) {
                    e.printStackTrace();
                }

What wrong I am doing. Since it accepts the username and password for one link and throws the error for the second link.. Any suggestions will be appreciated...

我在做什么错。由于它接受一个链接的用户名和密码,并为第二个链接抛出错误.. 任何建议将不胜感激...

回答by Steve Perkins

This questionmay address the very same issue. Have you tried hitting the same page in a regular browser, while monitoring the requests and responses with a tool like Firebug(my personal favorite).

这个问题可能会解决同样的问题。您是否尝试过在常规浏览器中访问同一页面,同时使用Firebug(我个人最喜欢的)等工具监视请求和响应。

There may actually not be an issue at all. If that's the case, then you can set ALLOW_CIRCULAR_REDIRECTS in the client params.

实际上可能根本没有问题。如果是这种情况,那么您可以在客户端参数中设置 ALLOW_CIRCULAR_REDIRECTS。

UPDATE:

更新:

To allow for circular redirects, your code would look like this...

为了允许循环重定向,您的代码将如下所示...

...
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
...