gpt4 book ai didi

iphone - UIWebView 查看自签名网站(没有私有(private) api,不是 NSURLConnection)——这可能吗?

转载 作者:IT王子 更新时间:2023-10-29 07:36:48 26 4
gpt4 key购买 nike

有很多这样的问题:我可以获取 UIWebView 来查看自签名的 HTTPS 网站吗?

答案总是涉及:

  1. 使用私有(private) api 调用 NSURLRequest:allowsAnyHTTPSCertificateForHost
  2. 改用 NSURLConnection 和委托(delegate) canAuthenticateAgainstProtectionSpace

对我来说,这些不行。
(1) - 意味着我无法成功提交到应用商店。
(2) - 使用 NSURLConnection 意味着 CSS、图像和其他东西在收到初始 HTML 页面后必须从服务器获取的内容不会加载。

请问有没有人知道如何使用UIWebView查看自签名的https网页,不涉及上述两种方法?

或者 - 如果使用 NSURLConnection 实际上可以用来呈现包含 CSS、图像和其他所有内容的网页 - 那会很棒!

干杯,
拉伸(stretch)。

最佳答案

我终于明白了!

你可以做的是:

照常使用 UIWebView 发起您的请求。然后 - 在 webView:shouldStartLoadWithRequest 中 - 我们回复NO,而是使用相同的请求启动 NSURLConnection。

使用 NSURLConnection,您可以与自签名服务器通信,因为我们能够通过 UIWebView 不可用的额外委托(delegate)方法来控制身份验证>。因此,使用 connection:didReceiveAuthenticationChallenge 我们可以针对自签名服务器进行身份验证。

然后,在 connection:didReceiveData 中,我们取消 NSURLConnection 请求,并使用 UIWebView 再次启动相同的请求 - 现在可以工作了, 因为我们已经通过了服务器认证:)

下面是相关的代码片段。

注意:您将看到的实例变量属于以下类型:
UIWebView *_web
NSURLConnection *_urlConnection
NSURLRequest *_request

(我为 _request 使用实例变量,因为在我的例子中它是一个包含大量登录详细信息的 POST,但如果需要,您可以更改为使用传入的请求作为方法的参数。 )

#pragma mark - Webview delegate

// Note: This method is particularly important. As the server is using a self signed certificate,
// we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the
// request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods
// which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete
// the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated);

if (!_authenticated) {
_authenticated = NO;

_urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];

[_urlConnection start];

return NO;
}

return YES;
}


#pragma mark - NURLConnection delegate

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection");

if ([challenge previousFailureCount] == 0)
{
_authenticated = YES;

NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];

} else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection");

// remake a webview call now that authentication has passed ok.
_authenticated = YES;
[_web loadRequest:_request];

// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
}

// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

我希望这可以帮助其他人解决我遇到的同样问题!

关于iphone - UIWebView 查看自签名网站(没有私有(private) api,不是 NSURLConnection)——这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11573164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com