PostMethod提交302錯誤
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;
}
最後更新:2017-04-02 15:28:22