android的重定向问题

Ronys 2013-03-06 02:41:32
new Thread() {
public void run() {
try {
/*
* NameValuePair代表一个HEADER,List<NameValuePair>
* 存储全部的头字段
* UrlEncodedFormEntity类似于URLEncoder语句进行URL编码
* HttpPost类似于HTTP的POST请求
* client.execute()类似于发出请求,并返回Response
*/
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(
HttpResponse response,
HttpContext context) {
Log.e("isRedirectRequested_response code:",
response.getStatusLine()
.getStatusCode() + "");
return false;
}
@Override
public URI getLocationURI(
HttpResponse response,
HttpContext context)
throws ProtocolException {
// TODO Auto-generated method stub
return null;
}
});
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair pair1 = new BasicNameValuePair(
"name", name.getText().toString());
NameValuePair pair2 = new BasicNameValuePair(
"age", age.getText().toString());
list.add(pair1);
list.add(pair2);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
list, "UTF-8");
HttpPost post = new HttpPost(url);
post.setEntity(entity);
HttpResponse response = client.execute(post);
Log.e("response code:", response
.getStatusLine().getStatusCode() + "");
if (response.getStatusLine().getStatusCode() == 200) {
InputStream in = response.getEntity()
.getContent();// 接收服务器的数据,并在Toast上显示
String str = readString(in);
Log.e("response_post:", str);
} else {
Log.e("response_post:", "POST提交失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();

以上这段代码是模拟post请求,假设发生重定向,本来 response.getStatusLine().getStatusCode() 应该返回302的,可是不知道为什么总是返回200,求解。我已进行重定向的处理:http://blog.sina.com.cn/s/blog_60d2d62a01011shn.html#post
还有个问题,为什么用HttpURLConnection获取不到完整额网页源码,上一段也是一样获取不到全部的网页源码,是怎么回事呢?
...全文
63 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ronys 2013-03-06
  • 打赏
  • 举报
回复
自己先顶一个!

80,352

社区成员

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

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