android的重定向问题
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获取不到完整额网页源码,上一段也是一样获取不到全部的网页源码,是怎么回事呢?
android重定向 --------------------编程问答-------------------- 自己先顶一个!
补充:移动开发 , Android