Obtaining URL from http response when no location header is sent
12,457
It should be similar to:
HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpClientParams.setRedirecting(params, false);
HttpGet method = new HttpGet("http://forecast.weather.gov/zipcity.php?inputstring=90210");
HttpResponse resp = client.execute(method);
String location = resp.getLastHeader("Location").getValue();
EDIT: I had to make a couple minor tweaks, but I tested and the above works.
Related videos on Youtube
Author by
joepetrakovich
Updated on June 04, 2022Comments
-
joepetrakovich almost 2 years
When communicating with http to http://forecast.weather.gov/zipcity.php I need to obtain the URL that is generated from a request.
I have printed out the headers and their values from the http response message but there is no location header. How can I obtain this URL? (I'm using HttpClient)
-
joepetrakovich over 13 yearsHow were you able to find that out? I am using the .getAllHeaders() function and printing them all out and a location header was not listed.
-
Matthew Flaschen over 13 years@Petra, using LiveHttpHeaders and Firebug. I will post an example shortly. Which version of HttpClient are you using?
-
joepetrakovich over 13 yearsthe org.apache.http.client module on Android 2.2
-
joepetrakovich over 13 yearsI downloaded those tools you mentioned and they are awesome! It appears the first response is a redirection to the URL that I am looking for, but when I communicate with the server via HttpClient it seems to be giving me the last http response with a 200 status. Any ideas on obtaining the header info for that very first http response message?
-
joepetrakovich over 13 yearsThat looks perfect, what does the .setRedirecting() function doing? I could look it up but since you may be active... My first guess would be that it stops all future http communication after that very first redirection response?
-
Matthew Flaschen over 13 years@Petra, that's correct. It just tells the library not to follow the redirects on its own. See also this answer; however, you don't need to create a new params object.