Java 使用HtmlUnit读取所有响应头
我试图使用http单元读取应用程序的响应头-Java 使用HtmlUnit读取所有响应头,java,htmlunit,Java,Htmlunit,我试图使用http单元读取应用程序的响应头- WebClient webClient = new WebClient(); WebClient.setThrowExceptionOnScriptError(false); HtmlPage currentPage = webClient.getPage("http://myapp.com"); WebResponse response = currentPage.getWebResponse(); System.out.println(respo
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头,而不仅仅限于第一次获取 好的,这应该可以:
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变量,使其保持原始值。List response=currentPage.getWebResponse().getResponseHeaders();
List<NameValuePair> response =currentPage.getWebResponse().getResponseHeaders();
for (NameValuePair header : response) {
System.out.println(header.getName() + " = " + header.getValue());
}
for(NameValuePair标题:响应){
System.out.println(header.getName()+“=”+header.getValue());
}
对我来说效果很好。下面是一个简洁的示例,显示了使用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());
}
我最终使用了Selenium捕获n/w流量。