我使用的是网络服务,这是一个无效的证书的服务器上,但我想反正得到响应。 有没有办法做到这一点? 下面是示例代码:
-(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 =}
谢谢,