6

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)

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

6 Comments

How 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.
@Petra, using LiveHttpHeaders and Firebug. I will post an example shortly. Which version of HttpClient are you using?
the org.apache.http.client module on Android 2.2
I 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?
That 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?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.