我无法查看具有自签名证书且还需要 HTTP 身份验证的网站。目前我正在尝试通过使用如何在 UIWebView 中显示身份验证挑战来实现它?和UIWebView 来查看自签名网站(没有私有 api,不是 NSURLConnection) - 有可能吗?作为如何实现这一点的指南。我也在尝试使用绕过自签名证书的私有 api 方法,但我无法找到指向它的链接。但私有 api 标头是:
@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
然后我将这些作为重要功能:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated);
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
_request=[NSURLRequest requestWithURL:URL];
if (!_authenticated) {
_authenticated = NO;
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
_urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[_urlConnection start];
[mainWebView loadRequest:_request];
return NO;
}
return YES;
}
基本上调用 nsurl 连接来传递登录凭据。
#pragma mark - NURLConnection delegate
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection");
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
if ([challenge previousFailureCount] == 0)
{
_authenticated = YES;
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
NSLog(@"credential created");
} else
{
NSLog(@"previous authentication failure");
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
和
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection");
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"remote url returned error %d %@",[httpResponse statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
NSLog(@"The response is =%@",response);
_authenticated = YES;
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:URL];
[mainWebView loadRequest:urlRequest];
[_urlConnection cancel];
}