为什么不';Android web请求是否自动遵循重定向?
几周前,我在一个WebView中遇到了一个问题,它没有像普通浏览器那样跟踪重定向。我在许多答案中使用了以下建议:为什么不';Android web请求是否自动遵循重定向?,android,redirect,apache-httpclient-4.x,Android,Redirect,Apache Httpclient 4.x,几周前,我在一个WebView中遇到了一个问题,它没有像普通浏览器那样跟踪重定向。我在许多答案中使用了以下建议: String newUrl = response.getFirstHeader("Location").getValue(); 但它只提供了一步重定向,但没有提供更多重定向,这是它需要的。我通过反复监听重定向并手动完成每一步来绕过它 现在我使用以下代码: HttpClient httpClient = MyApp.getHttpClient(); HttpPost httpPost
String newUrl = response.getFirstHeader("Location").getValue();
但它只提供了一步重定向,但没有提供更多重定向,这是它需要的。我通过反复监听重定向并手动完成每一步来绕过它
现在我使用以下代码:
HttpClient httpClient = MyApp.getHttpClient();
HttpPost httpPost = new HttpPost(con.getString(R.string.platform_url_getBalances));
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("sid", String.valueOf(sessionKey)));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String response = null;
// Execute HTTP Post Request. Response body returned as a string
response = httpClient.execute(httpPost, responseHandler);
HttpClient-HttpClient=MyApp.getHttpClient();
HttpPost-HttpPost=newhttppost(con.getString(R.string.platform_url_getBalances));
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“sid”,String.valueOf(sessionKey)));
ResponseHandler ResponseHandler=新BasicResponseHandler();
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
字符串响应=null;
//执行HTTP Post请求。作为字符串返回的响应正文
response=httpClient.execute(httpPost,responseHandler);
最近,R.string.platform\u url\u getBalances
的端点发生了变化,但我们将302重定向到了另一个url。它在浏览器和iPad版本的应用程序中运行良好,但对于Android,我得到org.apache.http.client.HttpResponseException:notfound
我觉得很奇怪,Android在重定向方面如此痛苦。为什么会这样,有没有合理的解决办法 事实上,它是为HttpClient 4.1设计的,就我所知很接近,而安卓则更老
我猜Android版本的HttpClient中的等效过程是:
- 创建一个子类
,根据您的应用程序覆盖DefaultRedirectHandler
isRedirectRequested()
- 创建
的子类,并重写DefaultHttpClient
以返回在上一步中创建的子类的实例createRedirectHandler()
- 使用
的子类替换DefaultHttpClient
本身,无论您在何处创建该实例DefaultHttpClient