0

I have an HTML content which includes an <img>. This <img> has an image url which will display the content only if the user is authenticated.

I have created the WebView as follows:-

wvDetail = FindViewById<WebView>(Resource.Id.wvDEtail);
        var data = AppHelper.SelectedNews;
        wvDetail.Settings.JavaScriptEnabled = true;
        var mData = "<p>​<span style=\"color &#58;#000000;font-family&#58;&quot;open sans&quot;, arial, sans-serif;text-align&#58;justify;background-color&#58;#ffffff;\">Proin sem urna, aliquet vel placerat quis, semper et mauris. Sed vel nunc lacus. Vestibulum vitae nisl eros. Donec ullamcorper sem nisl, in pellentesque quam tempus vel. Curabitur in felis nec urna placerat finibus. Donec ut nisi eu enim tincidunt luctus. Phasellus eleifend tortor est, nec maximus est aliquet sit amet. Praesent mollis massa id lacinia volutpat. Vestibulum eget odio sit amet enim fringilla pulvinar.​</span></p><p><br></p><p><img src=\"http://example.com/somesecuredimage.png" alt=\"somesecuredimage.png\" style=\"margin&#58;5px;width&#58;640px;\" /><br></p><p><br></p><p><br></p><p><br></p><p><br></p>";
        wvDetail.LoadData(mData, "text/html", "utf-8");

I have an access token which I need to set as bearer to the Authorization header or pass as cookie to this Webview in order to load all the secured images included in the html content.

I have seen solutions where we can pass the headers as a parameter but with the LoadUrl(). Is there a way I can do the same while loading the HTML text instead?

Any help is appreciated

user3034944
  • 1,541
  • 2
  • 17
  • 35

1 Answers1

0

the answer does not show how to pass an authorization token.

You can create you own Android.Webkit.WebViewClient and override the ShouldInterceptRequest Method in order to intercept every request, even you're using LoadData method.

Since I don't know your format of passing an authorization token, here I can only show way to create such a WebViewClient.

Create a MyWebViewClient which inherit from WebViewClient

public class MyWebViewClient : WebViewClient
{
    public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    {
        return base.ShouldInterceptRequest(view, request);

        //TODO: Pass your authorization token here. 
    }
}

And then in your Activity:

wvDetail = FindViewById<WebView>(Resource.Id.wvDEtail);
wvDetail.Settings.JavaScriptEnabled = true;
wvDetail.SetWebViewClient(new MyWebViewClient());
var mData = "YOUR-DATA";
wvDetail.LoadData(mData, "text/html", "utf-8");
Grace Feng
  • 16,564
  • 2
  • 22
  • 45