java 未发送位置标头时从 http 响应中获取 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4074400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Obtaining URL from http response when no location header is sent
提问by joepetrakovich
When communicating with http to http://forecast.weather.gov/zipcity.phpI need to obtain the URL that is generated from a request.
当与 http 通信到http://forecast.weather.gov/zipcity.php 时,我需要获取从请求生成的 URL。
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)
我已经从 http 响应消息中打印出标头及其值,但没有位置标头。我怎样才能获得这个网址?(我正在使用 HttpClient)
回答by Matthew Flaschen
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.
编辑:我不得不做一些小的调整,但我测试了上面的工作。