2017-01-26 333 views
6

我使用shouldInterceptRequest安卓的WebView shouldInterceptRequest在WebView中不添加RequestProperties

下面

是我給我回WebResourceResponse

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    private static WebResourceResponse handleRequestViaUrlOnly(WebResourceRequest webResourceRequest){ 
     String url = webResourceRequest.getUrl().toString(); 
     Log.i("intercepting req....!!!", url); 
     String ext = MimeTypeMap.getFileExtensionFromUrl(url); 
     String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext); 

     try { 
      HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); 
      conn.setRequestProperty("Sample-Header", "hello"); 
      conn.setDoOutput(true); 
      conn.setDoInput(true); 
      conn.setUseCaches(false); 
      return new WebResourceResponse(mime, "UTF-8", conn.getInputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

我把這個方法我CustomWebViewClient內部代碼攔截來自網頁流量請求

class CustomWebViewClient extends WebViewClient { 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    @Override 
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { 
     return handleRequestViaUrlOnly(request); 
    } 
} 

但是,當我檢查來自WebView遠程調試器的請求頭在chrome:// inspect /#設備。

我添加的附加RequestProperty不存在。

conn.setRequestProperty("Sample-Header", "hello"); 

Sample-Header不存在於WebView中的請求標題中。

我錯過了什麼嗎?我會感謝任何幫助。

+0

劑量它會在IO異常塊? –

+0

不會調用這個新的URL(url).openConnection()需要捕獲IOException。 – Aaron

+0

只需發佈調用此方法的完整代碼? –

回答

0

所以問題是,當你通過conn.getInputStream()它只給出數據。響應標題可以由conn.getHeaderFields()提取。除非服務器支持它,否則你將無法獲得額外的頭文件,並且不涉及CORS。這裏是連接

GET /~fdc/sample.html HTTP/1.1 
Sample-Header: hello 
Content-Type: application/x-www-form-urlencoded 
User-Agent: Dalvik/2.1.0 (Linux; U; Android 7.1; Android SDK built for x86_64 Build/NPF26K) 
Host: www.columbia.edu 
Connection: Keep-Alive 
Accept-Encoding: gzip 
Content-Length: 0 

HTTP/1.1 200 OK 
Date: Wed, 01 Mar 2017 09:06:58 GMT 
Server: Apache 
Last-Modified: Thu, 22 Apr 2004 15:52:25 GMT 
Accept-Ranges: bytes 
Vary: Accept-Encoding,User-Agent 
Content-Encoding: gzip 
Content-Length: 8664 
Keep-Alive: timeout=15, max=99 
Connection: Keep-Alive 
Content-Type: text/html 

正如你可以看到有響應無Sample-Header: hello頭的wireshark輸出。

下面是簡單的代碼,將構建從響應WebResourceResponse頭和您的自定義標題附加在它:

webView.setWebViewClient(new WebViewClient() { 
    private Map<String, String> convertResponseHeaders(Map<String, List<String>> headers) { 
     Map<String, String> responseHeaders = new HashMap<>(); 
     responseHeaders.put("Sample-Header", "hello"); 

     for (Map.Entry<String, List<String>> item : headers.entrySet()) { 
      List<String> values = new ArrayList<String>(); 

      for (String headerVal : item.getValue()) { 
       values.add(headerVal); 
      } 
      String value = StringUtil.join(values, ","); 
      Log.e(TAG, "processRequest: " + item.getKey() + " : " + value); 

      responseHeaders.put(item.getKey(), value); 
     } 

     return responseHeaders; 
    } 

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
    @Override 
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { 
     final String method = request.getMethod(); 
     final String url = request.getUrl().toString(); 
     Log.d(TAG, "processRequest: " + url + " method " + method); 
     String ext = MimeTypeMap.getFileExtensionFromUrl(url); 
     String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext); 

     try { 
      HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection(); 
      conn.setRequestMethod(method); 
      conn.setRequestProperty("Sample-Header", "hello"); 
      conn.setDoInput(true); 
      conn.setUseCaches(false); 

      Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields()); 

      responseHeaders.put("Sample-Header", "hello"); 

      return new WebResourceResponse(
        mime, 
        conn.getContentEncoding(), 
        conn.getResponseCode(), 
        conn.getResponseMessage(), 
        responseHeaders, 
        conn.getInputStream() 
        ); 

     } catch (Exception e) { 
      Log.e(TAG, "shouldInterceptRequest: " + e); 
     } 
     return null; 
    } 
}); 
+0

我也有這樣的代碼,是的,我得到的響應標題,我的問題是請求標題。我無法將請求標題添加到我的webview。另外,如果我在添加一個日誌之後記錄請求標頭,我將它們放在日誌中,但是當它到達webview時它不存在, – Aaron

+0

因此,您正在嘗試修改webResourceReqest以及在JavaScript端如何反映這些更改網頁流量? – j2ko