【问题标题】:What might cause a CircularRedirectException when accessing a password-protected URL with Apache DefaultHttpClient?使用 Apache DefaultHttpClient 访问受密码保护的 URL 时,什么可能导致 CircularRedirectException?
【发布时间】:2013-07-31 04:41:50
【问题描述】:

我正在尝试访问需要身份验证的页面。所以我在代码中传递了我的用户名和密码。这是我得到的以下输出和错误。首先它执行请求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: qbat.ts.notify@somehost.com
# 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: qbat.ts.notify@somehost.com
# 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/

这是我的验证码..

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();
                }

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

【问题讨论】:

  • 这并不能回答您的问题,但我建议您重新措辞标题。如果您只是将堆栈跟踪的一部分剪切并粘贴为问题标题,那么大多数人会跳过并忽略它。我会让标题更具描述性,例如“使用 Apache DefaultHttpClient 访问受密码保护的 URL 时,什么可能导致 CircularRedirectException?”
  • @Steve Perkins 我更新了标题。谢谢你告诉我。知道为什么会这样吗??

标签: java apache-httpclient-4.x


【解决方案1】:

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

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

更新:

要允许循环重定向,您的代码应如下所示...

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

【讨论】:

  • 感谢您的回复...是的,我正在使用萤火虫检查该页面,如果我输入我的用户名和密码,我会收到回复。但是在我得到的萤火虫中,对于与 302 Move Temporarily 相同的 url,然后是 302 Found,然后是 200 OK 请求,并且每个请求中的 url 都是相同的。以及如何在我的代码中使用这个ALLOW_CIRCULAR_REDIRECTS...
猜你喜欢
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
相关资源
最近更新 更多