2014-02-12 208 views
0
-(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?, 

回答

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中),它也會因爲同樣的原因拒絕連接。

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; 
}