3

在 MonoTouch 项目中,我有一个 UIWebView 加载一个基本身份验证保护的 url。通过手动添加基本身份验证标头,我设法针对对 url 的第一个请求进行身份验证:

UIWebView webView = new UIWebView(View.Frame);
... initialize webView
NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl("http://example.com"));
string header = "Basic " + Convert.ToBase64String (Encoding.UTF8.GetBytes("domain\\username:password"));
request["Authorization"] =  header;
webView.LoadRequest(request);`

它在 UIWebView 中打开的第一页中运行良好。但是,当在 web 视图中单击 href 链接时,请求失败并显示 401。我通过 Fiddler 检查了发送的标头,并且在第一个请求之后的任何后续请求中都没有发送我的标头。我尝试将标头注入到 ShouldStartLoad 委托中的所有请求中,但没有结果:

webView.ShouldStartLoad = (w, request, navType) => {
    ((NSMutableUrlRequest)request)["Authorization"] = header;
    return true;
};

我应该如何将 Authorization http 标头注入 UIWebView 中的所有请求?

4

0 回答 0