If you run the following code, you will see that the URI has a value of “null” for Host upon completion. This is invalid! It should always have a URI after executing a request.
I am trying to get the current page from the context after the page is requested (in case redirects occurred).
I apologize for the confusion. I mentioned the redirect because I thought it would help illustrate my problem more easily. The code below should work for pages that were redirected and those that were not.
Plug in your favorite site, it doesn’t matter. The final solution has to work for ANY site… those that were redirected and those that were not.
What am I missing?
public static void main(String args[]) throws ClientProtocolException, IOException
{
HttpParams httpParams = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpGet httpGet = new HttpGet("http://www.google.com/");
HttpContext context = new BasicHttpContext();
httpclient.execute(httpGet, context);
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
System.out.println("New URI host (why is it null?): " + currentReq.getURI().getHost());
}
From the docs (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html)