iOS / XCode NSURLConnection给403禁止

时间:2017-03-04 09:48:48

标签: ios xcode ssl

我正在尝试连接到我们目前没有有效证书的开发服务器之一。所以基本上,我想绕过目前的证书验证。 我的info.plist文件中已经有NSAppTransportSecurity&还覆盖了didReceiveAuthenticationChallenge,但没有用。

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https:testserver.com"]];
[NSURLRequest allowsAnyHTTPSCertificateForHost:@"testserver.com" ];
NSMutableURLRequest *mutableReq = [req mutableCopy];
[mutableReq setHTTPMethod:@"GET"];
[mutableReq setValue:@"Basic someKey" forHTTPHeaderField:@"Authorization"];
  NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:mutableReq delegate:self];

didReceiveAuthinticationChallenge正在调用,它看起来像这样:

(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
             [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

info.plist文件:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我从我的服务器上禁止403,而从Android做类似的事情,信任所有证书工作正常。 任何帮助将受到高度赞赏!

NSLog消息如下所示:

Connection failed:Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={_kCFStreamErrorCodeKey=-9802, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x174649120 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSErrorFailingURLStringKey=https://testserver.com, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamPropertySSLClientCertificateState=0, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., _kCFStreamErrorDomainKey=3, NSErrorFailingURLKey=https://testserver.com _kCFStreamErrorCodeKey=-9802}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.

0 个答案:

没有答案