【问题标题】:webService request on https url with self signed certificate doesn't work?带有自签名证书的 https url 上的 webService 请求不起作用?
【发布时间】:2016-05-29 02:25:12
【问题描述】:

我正在尝试使用NSURLSessionNSURLSessionDataTask 调用Web 服务,该站点是HTTPS 并使用自签名证书,我尝试为NSURLSession 设置委托并调用其方法让请求继续,但它也不起作用。

我也在Info.plist 中设置了应用程序传输安全性,但它也不起作用,它仅用于测试目的,我知道使用自签名证书很危险,请帮助。

这里是调用网络服务的示例代码:

- (void) operationRequest{
    NSMutableArray *extraDataRequest = [[NSMutableArray alloc] init];
    [extraDataRequest addObject:[mPayRequest prepareExtraDataWithKey:systemConstantsObj.section andValue:@"Dinarak"]];

    NSNumber *defaultLanguage = [NSNumber numberWithInt:1];
    NSDictionary *newDatasetInfo = [mPayRequest mPayRequestJsonObject:deviceId extraData:extraDataRequest language:defaultLanguage operation:systemConstantsObj.systemConfiguration pinCode:[NSNull null] sender:deviceId senderType:systemConstantsObj.mobileSenderType];

    NSString *stringValues = [mPayRequest ToStringValues:deviceId extraData:extraDataRequest language:defaultLanguage operation:systemConstantsObj.systemConfiguration pinCode:[NSNull null] sender:deviceId senderType:systemConstantsObj.mobileSenderType];

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSMutableURLRequest *request = [WebService POST:stringValues initWithJsonString:newDatasetInfo];

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
        if (data != nil && error == nil) {
            [self parseResponse:data];
        }
        if (data == nil || error != nil) {
            NSLog(@"error %@", error);
        }
    }];

    [dataTask resume];
}

-(void)parseResponse:(NSData *)data{
    NSMutableArray *extraData = [mPayResponseResult parseResponseForOperation:systemConstantsObj.systemConfiguration withData:data];
    if (extraData != nil && [extraData count] > 0)
        [systemConstantsObj saveSystemConfigurations:extraData];
    else
        systemConstantsObj.systemConfigsList = nil;
}

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        if([challenge.protectionSpace.host isEqualToString:@"dinarakapp"]){
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
    }
}

- (BOOL)connection:(NSURLSession *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return YES;
}

- (void)connection:(NSURLSession *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        if([challenge.protectionSpace.host isEqualToString:@"dinarakapp"])
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

还有Info.plist的设置:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>https://80.90.171.183:8445/ps-mpay</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

【问题讨论】:

  • 任何解决方案的人?

标签: ios objective-c web-services https nsurlsession


【解决方案1】:

在主项目文件夹和项目测试文件夹中的 Info.plist 中添加以下代码。我们之前也有同样的问题。通过在主项目 Info.plist 中添加 AppTransportSecurity 来给我们同样的错误。当我们对这两个 Info.plist 文件进行更改时,它就起作用了。

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

【讨论】:

  • harsha yarabarla 不幸的是它也不起作用。
  • NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9802)
  • Error Domain=NSURLErrorDomain Code=-1200 “发生 SSL 错误,无法与服务器建立安全连接。” UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=你还是要连接到服务器吗?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey={type = immutable,{count = immutable] , 值 = ( 0 :
  • )},NSUnderlyingError=0x15ed2ac40 {错误域=kCFErrorDomainCFNetwork 代码=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=1, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalStreamValue=-980 3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates={type = immutable, count = 1, values = ( 0 : )}}}, NSLocalizedDescription=An发生 SSL 错误,无法与服务器建立安全连接:
猜你喜欢
  • 2012-07-19
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2014-05-05
  • 2020-04-24
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多