5

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?

3 Answers 3

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

works fine for me.

2

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

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.

1
  • I ended up in using capture n/w traffic using Selenium.
    – Tarun
    Feb 6, 2012 at 10:22

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.