我很努力地使用https请求网络服务。我得到这种错误:通过SSL请求网络服务
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](https://github.com/AFNetworking/AFNetworking/blob/master/README.md)或[MKNetworking](https://github.com/MugunthKumar/MKNetworkKit /blob/master/README.mdown),这使得这些问题更容易处理。检查:http://stackoverflow.com/questions/13077753/need-to-view-website-with-a-self-signed-certificate-and-http-authentication – 2013-02-15 12:36:32
此外,可能重复:http:// stackoverflow。 com/questions/933331 /如何使用nsurlconnection-to-connect-with-ssl-for-an-untrusted-cert – 2013-02-15 12:37:55
我不想使用其他库。另外在给定的其他问题解决方案 – Streetboy 2013-02-16 07:50:40