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

时间:2011-07-14 17:34:40

标签: java apache-httpclient-4.x

我正在尝试访问需要身份验证的页面。所以我在代码中传递了用户名和密码。这是我得到的以下输出和错误。首先,它执行请求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();
                }

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

1 个答案:

答案 0 :(得分:14)

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

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

UPDATE:

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

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