[英]Following redirects in HTTPResponse Android
我需要遵循HTTPost給我的重定向。 當我創建一個HTTPost,並嘗試讀取響應時,我得到重定向的頁面html。 我怎樣才能解決這個問題? 碼:
public void parseDoc() {
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, true);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"https://secure.groupfusion.net/processlogin.php");
String HTML = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("referral_page",
"/modules/gradebook/ui/gradebook.phtml?type=student_view"));
nameValuePairs.add(new BasicNameValuePair("currDomain",
"beardenhs.knoxschools.org"));
nameValuePairs.add(new BasicNameValuePair("username", username
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", password
.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String g = httppost.getURI().toString();
HttpResponse response = httpclient.execute(httppost);
HTML = EntityUtils.toString(response.getEntity());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String ResponseBody = httpclient.execute(httppost, responseHandler);
sting.setText(HTML);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
當服務器發送重定向時,它實際上是發送一個3xx響應代碼(通常為301或302),表示重定向,以及一個Location頭,告訴您新的位置。
因此,在您的情況下,您可以從HttpResponse對象獲取Location標頭,並使用它來發送另一個請求以在您登錄后檢索實際內容。例如:
String newUrl = response.getFirstHeader("Location").getValue();
只要為兩個請求重用相同的HttpClient對象,它就應該使用后續請求中登錄請求設置的任何cookie。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.