没有得到服务器的响应

时间:2014-02-12 06:20:02

标签: ios https nsurlconnection ssl-certificate

    -(void)postExample {

        NSURL *aUrl = [NSURL URLWithString:@"https://deluxecms.net/dev/api/index.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];    
          NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
                                                                 delegate:self];

          [request setHTTPMethod:@"POST"];
           NSString *postString =   
        @"method=getDocument&deviceId=11a6b75c30fb0420ed2fccbc9d9cdf80& 
        cipher=52adabcb60014477b4cc82f35a032533&version=1&lastSync=0";
         [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
        if(connection)
        {
        NSLog(@"Connection Successful");
        }
        else
        {
        NSLog(@"Connection could not be made");
        }
     }

执行此消息后,下面是显示的错误。

ConnectionExample[4869:11303] Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this 
server is invalid. You might be connecting to a server that is pretending to be “deluxecms.net” 
which could put your confidential information at risk." UserInfo=0x75c2cb0
{NSErrorFailingURLStringKey=https://deluxecms.net/dev/api/index.php,
NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,

2 个答案:

答案 0 :(得分:0)

服务器上的SSL证书无效:

$ curl https://deluxecms.net/
curl: (60) SSL certificate problem: Invalid certificate chain
More details here: http://curl.haxx.se/docs/sslcerts.html

您需要联系服务器的系统管理员并让他们修复证书。

此外,您只需在网络浏览器中访问https://deluxecms.net/(至少在Safari中),并且出于同样的原因拒绝连接。

答案 1 :(得分:0)

当您使用https请求时,请确保在使用NSURLConnection的位置添加此代码(NSURLConnection委托方法)。

#pragma mark http call methods

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {  

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])

        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
    return YES;
}