期望状态代码在300到400之间,但获得200个android httpclient

时间:2013-08-21 19:44:15

标签: android redirect android-webview httpclient

我正在尝试检测android redirected上的webview页面。经过多方努力,我发现重定向页面的状态代码位于300到400之间。我还检查了重定向页面“en.m.wikipedia.com/wiki/Automobile”的状态代码并得到了预期的结果。 但是我试图在Android webview中复制同样的内容。我不会得到30X而是得到200.

这是我的代码:

public class MyActivity extends Activity {
   private WebView webView;
   private myWebChromeClient mWebChromeClient;
   private myWebViewClient mWebViewClient;

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       webView = (WebView) findViewById(R.id.webView);

       mWebViewClient = new myWebViewClient();
       webView.setWebViewClient(mWebViewClient);

       // mWebChromeClient = new myWebChromeClient();
      //  webView.setWebChromeClient(mWebChromeClient);
      webView.getSettings().setJavaScriptEnabled(true);

      String url ="http://en.wikipedia.com/wiki/Automobile";
      webView.loadUrl(url);


}
  class myWebViewClient extends WebViewClient {
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        Log.v("url loaded "," "+url);

        try{
            HttpClient httpClient;
            HttpParams params = new BasicHttpParams();
            params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            httpClient = new DefaultHttpClient(params);
            HttpPost post = new HttpPost(url);
            HttpResponse response = httpClient.execute(post);
            Log.w("Response ","Status line : "+    response.getStatusLine().toString());

        }catch(Exception e){

        }
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

       return false;  
    }
}
}

这是我的输出:

 V/ url loaded: http://en.wikipedia.com/wiki/Automobile
 W/Response: Status line : HTTP/1.0 200 OK
 url loaded: http://en.wikipedia.org/wiki/Automobile
 Response: Status line : HTTP/1.0 200 OK
 url loaded: http://en.m.wikipedia.org/wiki/Automobile
 Response: Status line : HTTP/1.1 200 OK

先谢谢。

1 个答案:

答案 0 :(得分:2)

Apache DefaultHttpClient自动跟随重定向unless configured otherwise

  

ClientPNames.HANDLE_REDIRECTS ='http.protocol.handle-redirects':定义是否应自动处理重定向。此参数需要java.lang.Boolean类型的值。如果未设置此参数,HttpClient将自动处理重定向。