question

PaulAllington-9864 avatar image
0 Votes"
PaulAllington-9864 asked LeonLu-MSFT commented

Has the POST problem (being able to set headers on a webview for POSTs) been fixed?

We are trying to override the headers to include an authorization header in every request in a webview in android (using Xamarin forms)

We are using a FormsWebViewClient, and overriding:

     public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest request)

Following similar code to this: https://forums.xamarin.com/discussion/87944/custom-header-in-webview

However it seems it's widely found to be impossible to do this for POST requests (GET requests are fine), but is fine on IOS, as IWebResourceRequest doesn't include the Form content, so it can't be recreated.

Has this been fixed? Or will it be?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

You can try to use following ways to send custom post request in webview.

public override WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, string url)
        {
            if (!TextUtils.IsEmpty(InsertParams))
            {
                string mParams = InsertParams;
                InsertParams = null;

                try
                {
                    URL mUrl = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection)mUrl.OpenConnection();
                    connection.DoInput = true;
                    connection.DoOutput = true;
                    connection.UseCaches = false;
                    connection.RequestMethod = "POST";
                    connection.SetRequestProperty("resource", "android");
                    connection.SetRequestProperty("client", "clientapp");
                    DataOutputStream os = new DataOutputStream(connection.OutputStream);
                    os.WriteBytes(mParams);
                    os.Flush();
                    InsertParams = null;
                    return new WebResourceResponse("text/html", connection.ContentEncoding, connection.InputStream);
                }
                catch (Exception e)
                {
                    e.Message.ToString();
                }
            }

            return base.ShouldInterceptRequest(view, url);


        }


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks very much. In that example - what is InsertParams? Does that include the form parameters and fields?

0 Votes 0 ·

Yes, you can ignore it, just use string content to make a test.

0 Votes 0 ·

Oh this works without it yes, but it doesn't include the form parameters from a form posted within the webview...there's seemingly no way to get hold of those

0 Votes 0 ·
Show more comments