301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于:
301 redirect: 301 代表永久性转移(Permanently Moved)。
302 redirect: 302 代表暂时性转移(Temporarily Moved )。
这是很官方的说法,那么它们的区别到底是什么呢?
这里只说解决方法,不针对这2个状态进行详解。
/**
* 执行post提交
*
* @return
* @author SHANHY
*/
private static String getPostResponse(String url, Part[] parts) {
PostMethod mPost = new PostMethod(url);
mPost.addRequestHeader("Content", "text/html,charset=GBK");
mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost.getParams()));
try {
int statusCode = httpClient.executeMethod(mPost);
String responseBody = "";
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = mPost.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
responseBody = getPostResponse(location, parts);// 用跳转后的页面重新请求。
}
} else if (statusCode == HttpStatus.SC_OK) {
responseBody = mPost.getResponseBodyAsString();
}
// System.out.println(responseBody);
if (statusCode == HttpStatus.SC_OK) {// 成功
return null;
}
mPost.releaseConnection();
return responseBody;
} catch (HttpException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}