我需要帮助将以下内容转换为C#。我尝试了几种方法但仍然失败。我想使用以下方法获取响应cookie以进行身份验证。
(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
答案 0 :(得分:0)
好的,结果只是自己搞清楚,不是太糟糕。
[Foundation.Export("webView:decidePolicyForNavigationResponse:decisionHandler:")]
public virtual void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, Action<WKNavigationResponsePolicy> decisionHandler)
{
NSHttpUrlResponse response;
NSHttpCookie[] cookies_holder;
response = (NSHttpUrlResponse)navigationResponse.Response;
cookies_holder = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, response.Url);
foreach (var cookie in cookies_holder)
{
NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
}
decisionHandler(WKNavigationResponsePolicy.Allow);
}