【发布时间】:2013-02-15 09:51:29
【问题描述】:
我正在努力使用 https 请求 Web 服务。我收到这种错误:
An error occured : The certificate for this server is invalid. You might be connecting to a server that is pretending to be “SERVER_ADDRESS” which could put your confidential information at risk.
我没有使用 NSUrlConnectionDelegate。这是我的方法:
- (void)sendRequestWithUrl:(NSURL*)url block:(void (^)(NSDictionary *dict, NSError *error)) block{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
NSError* err = nil;
NSHTTPURLResponse* rsp = nil;
// Perform the request synchronously on this thread
NSData *rspData = [NSURLConnection sendSynchronousRequest:request returningResponse:&rsp error:&err];
if (rspData && err == nil) {
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:rspData options:NSJSONReadingMutableLeaves error:&err];
if(result) {
block(result, err);
} else {
block(nil, err);
}
}else{
DLog(@"Requesting URL: %@ An error occured : %@",url,[err localizedDescription]);
block(nil, err);
}
}
我该如何解决这个问题?
【问题讨论】:
-
在我看来,您在使用自签名证书时遇到了问题。对吗?我建议你换成AFNetworking或MKNetworking这样的库,这样可以更容易处理这类问题。检查:stackoverflow.com/questions/13077753/…
-
我不想使用其他库。同样在给定的其他问题解决方案中被贬低
-
在这种情况下,这可能会有所帮助:cocoanetics.com/2010/12/…
-
另外,这可能会有所帮助(1 岁的帖子):stackoverflow.com/questions/9458989/…。只需使用 google 就能找到令人惊叹的内容 ;)
标签: ios objective-c ios6 nsmutableurlrequest