https url上带有自签名证书的webService请求不起作用?

时间:2016-02-17 11:43:24

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

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

我也在NSURLSession中设置了App传输安全性,并且也没有用,并且仅用于测试目的,我知道使用自签名证书很危险,请任何帮助。

这是一个用于调用webservice的示例代码:

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

1 个答案:

答案 0 :(得分:0)

在主项目文件夹和项目测试文件夹中的Info.plist中添加以下代码。我们以前遇到过同样的问题。通过在Main项目Info.plist中添加AppTransportSecurity用于向我们提供相同的错误。当我们在两个Info.plist文件中进行更改时,它都能正常工作。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
相关问题