我使用的服務器上有一個無效證書的Web服務,但我想要得到響應。 有沒有辦法做到這一點? 這裏的示例代碼:如何繼續使用不受信任的服務URL?
-(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 =}
感謝,
連接到這些網站是一個巨大的安全風險,除非你只有一個例外,只有這個網站,只有這個網站。如果沒有,很多人都會很痛苦,假設這是分佈式的 – JTApps 2012-07-26 17:18:06