在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 nameValuePairs = new ArrayList(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 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。

尝试使用HttpGet方法

默认情况下,GetMethods将遵循来自http服务器的重定向请求。 可以通过调用setFollowRedirects(false)来禁用此行为。

有关更多信息,请参阅此

希望能帮助到你,

干杯