2013-08-21 55 views
0

我想在android webview上檢測redirected頁面。經過很多努力,我發現重定向頁面的狀態代碼在300到400之間。我還檢查了重定向頁面「en.m.wikipedia.com/wiki/Automobile」的狀態代碼,並獲得了期望的結果。 但我試圖複製samething Android中webview.I沒有得到30X,但得到200期望300到400之間的狀態代碼,但得到200 android httpclient

這裏是我的代碼:

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 

提前致謝。

回答

2

阿帕奇DefaultHttpClient自動跟隨重定向unless configured otherwise

ClientPNames.HANDLE_REDIRECTS = 'http.protocol.handle-重定向':定義是否重定向應當自動處理。該參數需要java.lang.Boolean類型的值。如果這個參數沒有設置,HttpClient會自動處理重定向。

+0

實現此,我得到的響應:V/URL加載:http://en.wikipedia.com/wiki/Automobile W /響應:狀態行:HTTP/1.0 301已移至永久 URL加載:HTTP ://en.wikipedia.org/wiki/Automobile 響應:狀態行:HTTP/1.0 200 OK 加載的URL:http://en.m.wikipedia.org/wiki/Automobile 響應:狀態行:HTTP/1.1 200 OK(仍未完成重定向)。 – vijay

+0

http://en.m.wikipedia.org/wiki/Automobile返回200,不重定向。 – msh

+0

我打開en.wikipedia.com/wiki/Automobile.And它被重定向到en.m.wikipedia.org/wiki/Automobile.Probably在服務器端,他們使用useragent.I需要知道所有重定向。如何我得到所有這些? – vijay

相關問題