2016-11-30 35 views
-2
  1. 我正在與WebView一起工作。現在我需要獲取網頁的來源。 我嘗試過JavascriptInterface,但源已受到javascript的影響(當我在chrome上使用「view-source:」時不一樣)。
  2. 我們可以使用CookieWebViewOkHttp?我登錄Google,我的Cookie保存在WebView,現在我想獲得一些數據,但在OkHttp不在WebView

回答

-1

您可以通過點擊該網頁的網址,通過DefaultHttpClient

爲得到它:

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client 
HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do 
HttpResponse response = httpclient.execute(httpget); // Executeit 
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); // Create an InputStream with the response 
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = reader.readLine()) != null) // Read line by line 
    sb.append(line + "\n"); 

String resString = sb.toString(); // Result is here 

is.close(); // Close the stream 

你必須添加一些超時PARAMS還有:

HttpParams httpParameters = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParameters,3000); // 3s max for connection 
HttpConnectionParams.setSoTimeout(httpParameters, 4000); // 4s max to get data 
HttpClient httpclient = new DefaultHttpClient(httpParameters); 

編輯:

您可以從網頁流量爲獲得餅乾:

@Override 
public void onPageFinished(WebView view, String url){ 
    String cookies = CookieManager.getInstance().getCookie(url); 
    Log.d(TAG, "All the cookies in a string:" + cookies); 
} 
+0

但我需要的WebView的cookie來訪問該頁面 – Scit

+0

看到我的編輯答案 –

+0

以及如何使用上OkHttpClient(或任何客戶端)餅乾嗎?該網站我想在只有登錄時才能訪問的頁面上獲取HTML(我已經登錄了WebView) – Scit