我很努力地使用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