【问题标题】:Reading all response headers using HtmlUnit使用 HtmlUnit 读取所有响应头
【发布时间】:2012-02-03 12:14:38
【问题描述】:

我试图使用 http 单元来读取我的应用程序的响应标头 -

WebClient webClient = new WebClient();
WebClient.setThrowExceptionOnScriptError(false);
HtmlPage currentPage = webClient.getPage("http://myapp.com");
WebResponse response = currentPage.getWebResponse();
System.out.println(response.getResponseHeaders());  

我确实可以看到响应标头,但它们仅限于第一个 http get 请求。 当我将 LiveHTTPHeaders 插件用于 firefox 插件时,我得到了所有的 get 请求和相应的标头响应。

有没有办法为所有后续请求获取 http 标头,而不仅限于第一次获取?

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:
    List<NameValuePair> response =currentPage.getWebResponse().getResponseHeaders();
    for (NameValuePair header : response) {
         System.out.println(header.getName() + " = " + header.getValue());
     }
    

    对我来说很好。

    【讨论】:

      【解决方案2】:

      这是一个简洁的示例,显示来自 HTMLUnit 响应的所有标题:

      WebClient client = new WebClient();
      HtmlPage page = client.getPage("http://www.example.com/");
      WebResponse response = page.getWebResponse();
      
      for (NameValuePair header : response.getResponseHeaders()) {
          System.out.println(header.getName() + " : " + header.getValue());
      }
      

      【讨论】:

        【解决方案3】:

        AFAIK,这应该可行:

        WebClient webClient = new WebClient();
        WebClient.setThrowExceptionOnScriptError(false);
        HtmlPage currentPage = webClient.getPage("http://myapp.com");
        WebResponse response = currentPage.getWebResponse();
        System.out.println(response.getResponseHeaders());
        // And even in subsequent requests
        currentPage = webClient.getPage("http://myapp.com");
        response = currentPage.getWebResponse();
        System.out.println(response.getResponseHeaders());
        

        此代码有效吗?如果是这样,那么您可能没有正确分配 currentPage 变量,因此它保持原始值。

        【讨论】:

        • 我最终使用 Selenium 捕获 n/w 流量。
        猜你喜欢
        • 1970-01-01
        • 2017-09-06
        • 1970-01-01
        • 2014-05-24
        • 1970-01-01
        • 2013-06-06
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        相关资源
        最近更新 更多