Tuesday, February 28, 2017

Android: WebView shouldInterceptRequest not adding RequestProperties in the WebView

Leave a Comment

I am intercepting requests from the webview using shouldInterceptRequest

below is my code for returning my 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;     } 

I call this method inside my CustomWebViewClient

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

However, when I check the Request Headers from the WebView remote debugger in chrome://inspect/#devices.

The additional RequestProperty that I added is not present.

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

The Sample-Header is not present in the Request Headers in the WebView.

Am I missing something? I'll appreciate any help.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment