Httpclient重定向android

时间:2013-08-12 11:55:13

标签: android httpclient

问题如下:
我使用 httpclient 将登录数据发布到服务器。

在我的桌面上使用带有活动标题的firefox时,服务器会将我重定向到url2 witk 302,然后使用302重定向到url3然后我就可以获得200 ok。在android我得到了相同的登录页面和200 OK。

然后我禁用了自动重定向处理得到了响应并看到302重定向,但是

String location = response.getHeaders("Location")[0].toString();”给了我相同的登录页面网址,而不是来自firefox的网址。

我不知道为什么会这样。

代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri[0]);
HttpResponse response;
String responseString = null;

HttpParams params = httpclient.getParams();
HttpClientParams.setRedirecting(params, false);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

// Adding namvalue pairs here - adding correct i'm sure

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();

if (statusLine.getStatusCode() == HttpStatus.SC_OK){                    
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       response.getEntity().writeTo(out);
       out.close();
       responseString = out.toString();
} else{
       if (statusLine.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
       {
          String location = response.getHeaders("Location")[0].toString();

       }
       response.getEntity().getContent().close();     
}      

Firefox标题:

enter image description here

我得到的是:location = / login /

0 个答案:

没有答案