3
    NSURL *url = [NSURL URLWithString:@"https://myUrlString.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    NSURLResponse *responseurl;
    NSError *err;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseurl error:&err]; 
    NSLog(@"data length:%u", data.length);
    NSLog(@"response:%@ , error:%@", responseurl, err);

我得到的回应是:-

data length:0
response:(null) , 

错误:错误域=NSURLErrorDomain 代码=-1202 “此服务器的证书无效。您可能正在连接到伪装成“myUrlString.com”的服务器,这可能会使您的机密信息面临风险。” UserInfo=0x8742d70 {NSErrorFailingURLStringKey=https:https://myUrlString.com,

NSLocalizedRecoverySuggestion=你还是要连接到服务器吗?,

NSErrorFailingURLKey=https://myUrlString.com,

NSLocalizedDescription=此服务器的证书无效。您可能正在连接到伪装成“myUrlString.com”的服务器,这可能会使您的机密信息面临风险。NSUnderlyingError=0x8745bf0 " The certificate for this server is invalid. You might be connecting to a server that is pretending to be “myUrlString.com” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=}

4

2 回答 2

2

您正在使用 Https 请求,因此您应该使用 ASIHTTPRequest 或者您可以尝试此代码

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:@"https:yoururl"]];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:getData];
于 2012-08-07T10:15:58.393 回答
1

请参阅技术说明 TN2232,了解如何正确解决 HTTPS 服务器信任问题,更重要的是,不该做什么。底线是他们鼓励您修复服务器以解决信任问题,而不是解决它。

如果您想暂时解决它(例如,您只是在使用诸如 Charles 之类的工具进行一些测试,您暂时拦截请求以执行诊断),您可以使用私有方法关闭此验证(请注意,如果此代码在应用程序中提交到 App Store,他们可能会因为使用私有 API 而拒绝它)或者您可以响应NSURLConnection委托方法

同样,要非常小心地发送绕过此重要警告的代码,但如果在测试应用程序期间必须这样做,那么上述方法之一可能会很有用。

于 2014-03-26T16:54:06.790 回答