“不受信任的服务器证书”在iPhone上 [英] "untrusted server certificate" on iPhone

查看:702
本文介绍了“不受信任的服务器证书”在iPhone上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个iPhone应用程序,我想连接到'HTTPS'服务器以获取一些信息。但是,我在控制台中收到错误

I am writing an iPhone application and I would like to connect to a 'HTTPS' server to get some information. However, I get the error in the console which is


NSUnderlyingError =错误域= kCFErrorDomainCFNetwork代码= -1202
UserInfo = 0x3e95cf0 此服务器的证书无效。您
可能连接到假冒example.com
的服务器,这可能会使您的机密信息面临风险。; }

NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1202 UserInfo=0x3e95cf0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be "example.com" which could put your confidential information at risk."; }

我如何信任证书并获取http状态代码200

How can I trust the certificate and get the http status code 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
    }


推荐答案

我认为添加不受信任的证书(至少在模拟器中)是不可能的,但您可以告诉您的NSURLConnection代表接受自签名证书(或通常不受信任的证书)

I think that adding an untrusted certificate is (at least in the simulator) not possible, but you can tell your NSURLConnection Delegate to accept self signed certificates (or generally untrusted ones)

以下链接帮助我解决了这个问题!

The following link helped me solve the issue!

如何使用NSURLConnection连接SSL以获取不受信任的证书?

这篇关于“不受信任的服务器证书”在iPhone上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆