我想得到输入一个网址后,跳转后的网址,用HttpURLConnection该怎么做,求助!
这是我的代码:麻烦大家看看,兄弟我先谢谢!!!!!!!!!
HttpURLConnection httpurlconnection = null;
URL url = null;
url = new URL("一个网址url“);
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setConnectTimeout(6*1000); //设置连接超时
String urlsting=httpurlconnection.getURL().toString();我个人认为左后一句不对,怎么等到网页跳转后得到跳转后的网址呢!
我不想用webview控件,那样不想一个应用程序,我想放到后台运行!thanks
这是我的代码:麻烦大家看看,兄弟我先谢谢!!!!!!!!!
HttpURLConnection httpurlconnection = null;
URL url = null;
url = new URL("一个网址url“);
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setConnectTimeout(6*1000); //设置连接超时
String urlsting=httpurlconnection.getURL().toString();我个人认为左后一句不对,怎么等到网页跳转后得到跳转后的网址呢!
我不想用webview控件,那样不想一个应用程序,我想放到后台运行!thanks
String httpUrl = url;
HttpGet request = new HttpGet(httpUrl);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = EntityUtils.toString(response.getEntity());
//
return str;
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ""; }
HttpURLConnection httpurlconnection = null;
URL url = null;
url = new URL("一个网址url“);httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setConnectTimeout(6*1000); //设置连接超时
responseCode = conn.getResponseCode();
switch (responseCode)
{
case HttpStatus.SC_MOVED_TEMPORARILY://302跳转 当然也可能是301、303、307
String urlsting = conn.getHeaderField("location");//这里也可能是相对路径,需要自己拼接
break;
default:
break;
}