我正在使用具有无效证书的服务器上的 Web 服务,但无论如何我都想获得响应。有办法吗??这是示例代码:
-(void)createHttpHeaderRequest:(serviceType)type {
NSMutableURLRequest * theRequest;
if (type==kServiceTypeTopAccessory) {
NSString * test = @"{\"GetVehicleInventory\": {\"ApplicationArea\": some Json object here}}}}}";
NSString * final = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)test, NULL, CFSTR(":/?#[]@!$&'()*+,;=\""), kCFStringEncodingUTF8);
NSString * sampleReq = [NSString stringWithFormat:@"https://myuntrutsedServer?XML_INPUT=%@",final];
// NSString * final = [sampleReq stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"JSON:%@",sampleReq);
theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sampleReq]];
}
NSLog(@"Request:%@",theRequest);
self.theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if (self.theConnection) {
NSLog(@"Service hit");
}
}
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error%@",error);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"Reached here");
}
我在 didFailWithError 方法中收到此错误: NSLocalizedDescription=此服务器的证书无效。您可能正在连接到伪装成“209.112.58.126”的服务器,这可能会使您的机密信息面临风险。,NSUnderlyingError=0x6877b40“此服务器的证书无效。您可能正在连接到伪装成的服务器是“209.112.58.126”,这可能会使您的机密信息面临风险。", NSURLErrorFailingURLPeerTrustErrorKey=}
谢谢,