0

I am trying to detect redirected page on android webview. After alot of effort , I found that status code for redirected page lies in between 300 and 400. I also checked status code for redirected page "en.m.wikipedia.com/wiki/Automobile" and got the desired result. But I am trying to replicate samething in Android webview.I don't get 30X but get 200.

Here is my code:

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;  
    }
}
}

Here is my output:

 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

Thanks in Advance.

vijay
  • 2,034
  • 3
  • 19
  • 38

1 Answers1

2

Apache DefaultHttpClient automatically follows redirects unless configured otherwise

ClientPNames.HANDLE_REDIRECTS='http.protocol.handle-redirects': defines whether redirects should be handled automatically. This parameter expects a value of type java.lang.Boolean. If this parameter is not set HttpClient will handle redirects automatically.

msh
  • 2,700
  • 1
  • 17
  • 23
  • Implementing this , I got the response: V/ url loaded: http://en.wikipedia.com/wiki/Automobile W/Response: Status line :HTTP/1.0 301 Moved Permanently 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 (Still complete redirection isn't caught). – vijay Aug 22 '13 at 03:11
  • I opened en.wikipedia.com/wiki/Automobile.And it gets redirected to en.m.wikipedia.org/wiki/Automobile.Probably in the server side,they are using useragent.I need to know all redirects.How could I get all these ? – vijay Aug 22 '13 at 03:23
  • Also look into this case, url loaded: http://www.autotrader.com/ Response: Status line : HTTP/1.1 200 OK url loaded: http://m.autotrader.com/?fromwwwsite Response: Status line : HTTP/1.1 200 OK loaded: http://m.autotrader.com/index.html Response: Status line : HTTP/1.1 200 OK. – vijay Aug 22 '13 at 03:26
  • I'm not sure what you are trying to say. Yes, for some URLs you get redirects, for other - you don't get redirects. Once you turn off automatic handling you are free to do whatever you want - follow, don't follow, anything. – msh Aug 22 '13 at 03:32
  • in case of autotrader.com, isn't it being redirected ? why am I getting status 200 . – vijay Aug 22 '13 at 03:40
  • If you are getting 200 and HANDLE_REDIRECT is turned off, then it is not redirected. – msh Aug 22 '13 at 04:00
  • An example how to do this see here: http://stackoverflow.com/a/1519487/1892046 . This works for me. – Bas Nov 21 '14 at 18:19