4

I'm trying to use a crosswalk embedded webview to display a web page, with some javascript. Because I need to add some headers to each request, I am intercepting the request with shouldInterceptLoadRequest, and making the request with OkHttp.

@Override
public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
    try {
        Log.i(App.TAG, url);
        return new WebResourceResponse("", "UTF-8", getUrl(url));
    } catch (Exception e) {
        e.printStackTrace();
        return super.shouldInterceptLoadRequest(view, url);
    }
}

InputStream getUrl(String url) throws IOException {
    Request request = new Request.Builder()
            .url(url)
            .addHeader("MyHeader","MyHeaderValue")
            .build();

    Response response = client.newCall(request).execute();

    return response.body().byteStream();
}

This code works as intended at first, but upon making an Ajax request, I get this error : [INFO:CONSOLE(0)] "XMLHttpRequest cannot load https://api.example1.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example2.com' is therefore not allowed access."

I do not get this error if I don't intercept the request, but then I loose the ability to add headers to the request.

Subhanshuja
  • 390
  • 1
  • 3
  • 20
EddBC
  • 71
  • 6
  • Your webview uses CORS, which means every request to a foreign domain (so every request in your context), will first make an OPTIONS call to check if the actual call is allowed. Then it will do the GET request. I guess by inerception you intercept after the OPTIONS call? – Patrick Nov 22 '18 at 15:14

0 Answers0