【问题标题】:HTTPS Service is not workingHTTPS 服务不工作
【发布时间】:2013-02-07 06:59:40
【问题描述】:

我想使用网络服务登录。

我的网站是基于 https 的。我正在使用以下教程。

http://agilewarrior.wordpress.com/2012/02/01/how-to-make-http-request-from-iphone-and-parse-json-result/ 

以下代码运行良好。

responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

但我的代码是

responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"https:myurl/login?userId=username&password=111111"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

它给出错误

Connection failed: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk." UserInfo=0xa294260 {NSErrorFailingURLStringKey=https://mywebsite/login?userId=username&password=111111, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://mywebsite/login?userId=username&password=111111, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk., NSUnderlyingError=0xa5befc0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0xa5768d0>}

【问题讨论】:

    标签: ios objective-c ios6 https nsurlrequest


    【解决方案1】:

    发生的情况是您的证书无效(从您的应用程序的角度来看)。如果您想冒险,可以添加以下代码。

    - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
        return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSArray * trustedHosts = @[@"your domain"];
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
        if ([trustedHosts containsObject:challenge.protectionSpace.host])
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    }
    

    因此证书将被忽略。 (记得用您的域替换“您的域”。)

    【讨论】:

    • 如何触发?
    【解决方案2】:

    这是您要连接的网站的问题,而不是代码的问题。如果它没有已知的证书,则会被视为安全风险。

    【讨论】:

    • 没问题。我found a similar question 可能会帮助你,它似乎有一些相关的答案。有一种方法可以忽略它,这是其他人在这里发布的,但不推荐。
    • @JMarsh 我在请求 Google Places 自动完成 API 时收到此错误。那么如何添加我的证书呢?
    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 2018-11-06
    • 2019-08-21
    • 1970-01-01
    • 2014-12-20
    • 2018-10-31
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多