无效的Cookie标头,然后它要求授权

时间:2011-07-14 03:12:47

标签: java httpclient web-crawler

我正在尝试抓取需要Siteminder身份验证的页面,所以我试图在代码本身中传递我的用户名和密码来访问该页面并继续抓取该页面中的所有链接。这是我的Controller.java代码。从这个MyCrawler类开始调用。

public class Controller {
    public static void main(String[] args) throws Exception {

            CrawlController controller = new CrawlController("/data/crawl/root");

            controller.addSeed("http://ho.somehost.com/");

            controller.start(MyCrawler.class, 10);  
            controller.setPolitenessDelay(200);
            controller.setMaximumCrawlDepth(3);
    }
}

这是我的MyCrawler.java代码。在这里,我传递我的凭据(用户名和密码)用于siteminder身份验证。并且只是想确保认证应该在这个MyCrawler代码或上面的Controller代码中完成.. ???此抓取工具代码来自此处(http://code.google.com/p/crawler4j/)

public class MyCrawler extends WebCrawler {

    Pattern filters = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g"
            + "|png|tiff?|mid|mp2|mp3|mp4" + "|wav|avi|mov|mpeg|ram|m4v|pdf"
            + "|rm|smil|wmv|swf|wma|zip|rar|gz))$");

    public MyCrawler() {


    }

    public boolean shouldVisit(WebURL url) {

        System.out.println("RJ:- " +url);

        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, null),
                    new UsernamePasswordCredentials("test", "test"));

            // Set timeout
            //client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
            HttpGet request = new HttpGet(url.toString());

            HttpResponse response = client.execute(request);
            if(response.getStatusLine().getStatusCode() == 200)
            {
                InputStream responseIS = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseIS));
                String line = reader.readLine();
                while (line != null)
                {
                    System.out.println(line);
                    line = reader.readLine();
                }
            }
            else
            {
                System.out.println("Resource not available");
            }
        }
        catch (ClientProtocolException e)
        {
            System.out.println(e.getMessage());
        }
        catch (ConnectTimeoutException e)
        {
            System.out.println(e.getMessage());
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            if ( client != null )
            {
                client.getConnectionManager().shutdown();
            }
        }


        String href = url.getURL().toLowerCase();
        if (filters.matcher(href).matches()) {
            return false;
        }
        if (href.startsWith("http://")) {
            return true;
        }
        return false;
    }

    public void visit(Page page) {
        int docid = page.getWebURL().getDocid();
        String url = page.getWebURL().getURL();         
        String text = page.getText();
        List<WebURL> links = page.getURLs();
        int parentDocid = page.getWebURL().getParentDocid();

        System.out.println("Docid: " + docid);
        System.out.println("URL: " + url);
        System.out.println("Text length: " + text.length());
        System.out.println("Number of links: " + links.size());
        System.out.println("Docid of parent page: " + parentDocid);
        System.out.println("=============");
    }   
}

我正在打印网址,以便我可以看到正在打印的网址。因此,通过这种方式,它打印两个url,一个需要身份验证的实际url,然后是一些siteminder url。当我运行这个项目时,我得到错误如下......

RJ:- http://ho.somehost.com/net/pa/ho.xhtml
 WARN [Crawler 1] Invalid cookie header: "Set-Cookie: SMCHALLENGE=; expires=Sat, 15 Jan 2011 02:52:54 GMT; path=/; domain=.somehost.com". Unable to parse expires attribute: Sat, 15 Jan 2011 02:52:54 GMT
 WARN [Crawler 1] Invalid cookie header: "Set-Co## Heading ##okie: SMIDENTITY=nzFSq2U3g/C3C6/jkj/Ocghyh/njK; expires=Sat, 13 Jul 2013 02:52:54 GMT; path=/; domain=.somehost.com". Unable to parse expires attribute: Sat, 13 Jul 2013 02:52:54 GMT
null
 INFO [Crawler 1] Number of pages fetched per second: 0
RJ:- https://lo.somehost.com/site/no/176/sm.exhtml
 WARN [Crawler 1] Invalid cookie header: "Set-Cookie: SMCHALLENGE=; expires=Sat, 15 Jan 2011 02:52:56 GMT; path=/; domain=.somehost.com". Unable to parse expires attribute: Sat, 15 Jan 2011 02:52:56 GMT
 WARN [Crawler 1] Invalid cookie header: "Set-Cookie: SMIDENTITY=IqsIPo; expires=Sat, 13 Jul 2013 02:52:56 GMT; path=/; domain=.somehost.com". Unable to parse expires attribute: Sat, 13 Jul 2013 02:52:56 GMT

任何建议都将受到赞赏。如果我将登录网址粘贴到浏览器中,那么它会询问用户名和密码,如果我输入我的用户名和密码,那么我会得到实际的屏幕。

1 个答案:

答案 0 :(得分:0)

为后代提取聊天讨论的显着内容,以防有人遇到同样的问题。

显示警告消息,表明HttpClient无法解析SiteMinder发出的Set-Cookie标头。使用Wireshark分析网络流量揭示了以下内容:

  • 没有为SiteMinder发布的Cookie SMSESSION设置expires属性。这不是问题的原因;这只是一个注意事项,需要查看负责警告的服务器的HTTP响应。
  • 针对Cookie SMCHALLENGESMIDENTITY发出了警告。因此,需要检查包含这两个cookie的Set-Cookie标头的响应。
  • 问题可能出在:
    • Cookie值本身,或
    • Cookie的expires属性中日期的格式。
  • HttpClient的
  • Bug no 923已在HttpClient的4.1.1版本中修复,可能包含分辨率。该修复程序用于支持2位和4位数年份,可能是问题的原因。

如果上述(使用cookie中的4位数年份值)证明是不正确的根本原因,那么必须指定用于解析cookie值的日期格式。这可以通过以下方式使用HttpClient指定允许/接受的日期格式列表来完成:

HttpGet request = new HttpGet(url.toString());
request.getParams().setParameter(CookieSpecPNames.DATE_PATTERNS, Arrays.asList("EEE, d MMM yyyy HH:mm:ss z"));
HttpResponse response = client.execute(request);

而不是现有的来电:

HttpGet request = new HttpGet(url.toString());

HttpResponse response = client.execute(request);

指定的模式EEE, d MMM yyyy HH:mm:ss z是看似错误解析的日期的有效模式(通过控制台中的消息)。如果HttpClient无法正确处理其他日期格式,则需要添加其他模式。有关所用格式的详细信息,请参阅SimpleDateFormat类文档。