HttpURLConnection的问题 高分求助

zjt0318 2011-08-16 03:16:38
我想得到输入一个网址后,跳转后的网址,用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
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoying_ 2011-08-17
  • 打赏
  • 举报
回复
自己报个班吧,要找老师的话
AspireHouse 2011-08-17
  • 打赏
  • 举报
回复
一般跳转后的地址都是在服务器返回的头字段location里面
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;
}
forqzy 2011-08-17
  • 打赏
  • 举报
回复
分析那个收到的 EntityUtils.toString(response.getEntity()), 就可以找到你要的东西

private String getDocuments(String url) {
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 "";

}
zjt0318 2011-08-16
  • 打赏
  • 举报
回复
求android高手当老师,我会认真跟着学习的》
zjt0318 2011-08-16
  • 打赏
  • 举报
回复
不好意思,打错字了,最后一句代码不对........大家见谅啊

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧