使用 HtmlUnit 读取所有响应标头

发布于 2025-01-01 15:59:16 字数 468 浏览 4 评论 0原文

我试图使用 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 标头,而不仅仅限于第一次获取?

I was trying to use http unit to read response header for my application -

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

I do get to see the response headers but they are limited to only first http get request.
When I used LiveHTTPHeaders plugin for firefox plugin I got to all the get requests and corresponding header responses.

Is there any way to get http header for all subsequent requests and not being limited to just first get?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲喜皆因你 2025-01-08 15:59:16
List<NameValuePair> response =currentPage.getWebResponse().getResponseHeaders();
for (NameValuePair header : response) {
     System.out.println(header.getName() + " = " + header.getValue());
 }

对我来说效果很好。

List<NameValuePair> response =currentPage.getWebResponse().getResponseHeaders();
for (NameValuePair header : response) {
     System.out.println(header.getName() + " = " + header.getValue());
 }

works fine for me.

浅浅 2025-01-08 15:59:16

下面是一个简洁的示例,显示了 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());
}

Here is a concise example displaying all the headers from a response with 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());
}
醉城メ夜风 2025-01-08 15:59:16

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 变量,因此它保留原始值。

AFAIK, this should work:

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

Does this code work? If it does, then you are probably not properly assigning the currentPage variable so it keeps holding the original values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文