我正在编写一个 iPhone 应用程序,我想连接到“HTTPS”服务器以获取一些信息。但是,我在控制台中收到错误消息
NSUnderlyingError = 错误域=kCFErrorDomainCFNetwork 代码=-1202 UserInfo=0x3e95cf0 "此服务器的证书无效。您可能正在连接一个伪装成“example.com”的服务器,这可能会使您的机密信息面临风险。"; }
如何信任证书并获取 http 状态码 200
以下是我的代码。
NSMutableURLRequest *request_get2 = [[[NSMutableURLRequest alloc] init] autorelease];
[request_get2 setURL:[NSURL URLWithString:@"https://www.example.com"]];
[request_get2 setHTTPMethod:@"GET"];
[request_get2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request_get2 setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request_get2 setValue:@"https://www.example.com" forHTTPHeaderField:@"Referer"];
[request_get2 setHTTPShouldHandleCookies:YES];
// cookiesString is in format "cookieName=cookieValue;"
[request_get2 setValue: (NSString *) cookiesString forHTTPHeaderField:@"Cookie"];
// doGet - response
NSHTTPURLResponse *response_get2 = nil; // it change from 'NSURLRespaonse' to 'NSHTTPURLResponse'
NSError *error_get2 = nil;
NSData *responseData_get2 = [NSURLConnection sendSynchronousRequest:request_get2 returningResponse:&response_get2 error:&error_get2];
NSString *data_get2 = [[NSString alloc]initWithData:responseData_get2 encoding:NSUTF8StringEncoding];
if (!error_get2) {
NSString *responseURL_get2 = [[response_get2 URL] absoluteString]; // null value
NSString *responseTextEncodingName_get2 = [response_get2 textEncodingName]; // null value
NSString *responseMIMEType_get2 = [response_get2 MIMEType]; // null value
NSUInteger *responseStatusCode_get2 = [response_get2 statusCode]; //[responseStatusCode intValue]; // the status code is 0
}
else {
NSLog(@"\nsomething went wrong: %@\n", [error_get2 userInfo]); // got the error in here
}