如何为WKWebView的请求设置自定义HTTP标头 [英] How to set custom HTTP headers to requests made by a WKWebView

查看:87
本文介绍了如何为WKWebView的请求设置自定义HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个包含WKWebView的应用程序,并且该Web视图加载的网站支持多种语言.如何更改WKWebView中的 Accept-Language 标头或其他HTTP标头?

I have built an app that includes a WKWebView, and the website that the web view loads supports multiple languages. How can I change the Accept-Language header in a WKWebView, or other HTTP headers for that matter?

推荐答案

我已经以某种方式使它工作了,但是只有get请求才具有自定义标头.当jbelkins在链接中回答时,Gabriel Cartiers对您的问题发表了评论,您将不得不操纵该请求并重新加载它.

I've got it working in a way, but only get requests will have the custom header. As jbelkins answered in the linked so from Gabriel Cartiers comment to your question, you will have to manipulate the request and load it anew.

我已经将它用于这样的GET请求:

I've got it working for GET-Requests like this:

(在xamarin 0> c#中,但我想您会明白的)

(it's in xamarin 0> c#, but i think you will get the idea)

我创建了一个私有字段

private bool _headerIsSet

每次使用deligate方法发出请求时,我都会检查一次:

which i check every time a request is made in the deligate method:

[Foundation.Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
    public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
    {
        var request = navigationAction.Request;
        // check if the header is set and if not, create a muteable copy of the original request
        if (!_headerIsSet && request is NSMuteableUrlRequest muteableRequest);              
        {
            // define your custom header name and value
            var keys = new object[] {headerKeyString};
            var values = new object[] {headerValueString};
            var headerDict = NSDictionary.FromObjectsAndKeys(values, keys);
            // set the headers of the new request to the created dict
            muteableRequest.Headers = headerDict;
            _headerIsSet = true;
            // attempt to load the newly created request
            webView.LoadRequest(muteableRequest);
            // abort the old one
            decisionHandler(WKNavigationActionPolicy.Cancel);
            // exit this whole method
            return;
        }
        else
        {
            _headerIsSet = false;                
            decisionHandler(WKNavigationActionPolicy.Allow);
        }
    }

正如我所说,这仅适用于 GET -Requests.不知何故, POST -请求不包含原始请求的正文数据(request.Body和request.BodyStream为null),因此MutableRequest(这是一个可静音的)原始请求的副本)将不包含原始请求的正文数据.

As i said, this only works for GET-Requests. Somehow, POST-Requests don't contain the body data of the original request (request.Body and request.BodyStream are null), so the muteableRequest (which is a muteable copy of the original request) won't contain the body data of the original request.

我希望这会帮助您或其他解决相同问题的人.

I hope this will help you or others that approach the same problem.

编辑:根据需要,将接受语言"设置为键

For your needs, set "Accept-Language" as the key

这篇关于如何为WKWebView的请求设置自定义HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆