【发布时间】:2016-05-29 02:25:12
【问题描述】:
我正在尝试使用NSURLSession 和NSURLSessionDataTask 调用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