Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining URL from http response when no location header is sent

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)

like image 431
joepetrakovich Avatar asked Nov 02 '10 01:11

joepetrakovich


1 Answers

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.

like image 75
Matthew Flaschen Avatar answered Oct 20 '22 00:10

Matthew Flaschen