0

我正在尝试检测redirectedandroid 上的页面webview。经过一番努力,我发现重定向页面的状态码在 300 到 400 之间。我还检查了重定向页面“en.m.wikipedia.com/wiki/Automobile”的状态码并得到了想要的结果。但我试图在 Android webview 中复制同样的东西。我没有得到 30 倍,而是得到了 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

提前致谢。

4

1 回答 1

2

Apache DefaultHttpClient 自动遵循重定向,除非另有配置

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

于 2013-08-21T20:14:15.923 回答