Reading all response headers using HtmlUnit

I tried to use the http module to read the 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 see response headers, but they are limited only by the first HTTP get request. When I used the LiveHTTPHeaders plugin for the firefox plugin, I received all the receive requests and the corresponding header responses.

Is there a way to get an HTTP header for all subsequent requests and not be limited to just the first?

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

works great for me.

+6
source

Here is a quick example displaying all response headers using 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()); } 
+2
source

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 so, then you are probably setting the currentPage variable incorrectly to preserve the original values.

+1
source

Source: https://habr.com/ru/post/1394603/


All Articles