2013-02-21 184 views
2

我想通過使用Web服務登錄。HTTPS服務不起作用

我的網站是基於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>} 

回答

1

這是要連接,而不是代碼的網站的問題。如果它沒有已知的證書,它將被視爲安全風險。

+0

我該如何解決它? – Arahim 2013-02-21 19:59:02

+0

ohh謝謝,這清除了很多。 – Arahim 2013-02-21 20:00:12

+0

沒問題。我[找到一個類似的問題](http://stackoverflow.com/questions/12347410/iphone-secure-restfull-server-the-certificate-for-this-server-is-invalid)可能會幫助你,看來就好像它有一些相關的答案。有一種方法可以忽略它,這是別人在這裏發佈的內容,但不推薦。 – JMarsh 2013-02-21 20:04:30

5

發生什麼事是您的證書無效(從您的應用程序的角度來看)。如果您想冒險,可以添加以下代碼。

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

所以證書將被忽略。 (請記住將您的域名替換爲您的域名。)

+0

它將如何被觸發? – Umitk 2016-06-03 09:22:27