How to prevent Origin:null in android webview

Viewed 272

I am trying to do webview.postUrl in android code

it automatically adds Origin:null in the headers

How to prevent this?

My Code below

import butterknife.Bind;
.....
@Bind(R.id.webview)
WebView webview;
....
....
protected final void loadUrl(String url) {
    webview.postUrl("http://localhost:8080/myapp/login", mypostdata);
}
1 Answers

The Origin is set to null probably because of CORS (Cross-Origin Resource Sharing) that is enabled on the API you're requesting.

Check this page for more information

AFAIK, the postUrl method does not allow updating Origin.

I managed to send a custom Origin URL using the following workaround :

val originURL = "https://www.origin.url/"
val htmlContent = """
    <html>
        <body onload='form1.submit()'>
            <form id='form1' action='http://localhost:8080/myapp/login' method='POST'>
                <input name='POST_PARAM_1' type='hidden' value='VALUE_OF_PARAM1' />
                <input name='POST_PARAM_2' type='hidden' value='VALUE_OF_PARAM2' />
                <input name='POST_PARAM_3' type='hidden' value='VALUE_OF_PARAM3' />
                <input name='POST_PARAM_4' type='hidden' value='VALUE_OF_PARAM4' />
            </form>
        </body>
    </html>
""".trimIndent()

webView.loadDataWithBaseURL(originURL , htmlContent ,  "text/html" , "UTF-8" , null)

Instead of using postUrl which doesn't support sending custom headers, I called loadDataWithBaseURL, this method expects a baseURL (which is the Origin URL).

Based on the documentation:

Loads the given data into this WebView, using baseUrl as the base URL for the content. The base URL is used both to resolve relative URLs and when applying JavaScript's same-origin policy. The historyUrl is used for the history entry.

The WebView will load an HTML page that automatically POSTs parameters to your URL.

Related