我在 iPhone 应用程序中使用以下代码通过 https 与服务器进行身份验证。
身份验证正在 Web 浏览器上进行,但是当我尝试从 iPhone 应用程序进行身份验证时,服务器响应代码为 404。 使用下面的代码,通过 http 从 iPhone 应用程序成功进行身份验证。
所以我想知道 iOS 5 中的 https 身份验证是否有重大变化。
我的代码如下。
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
if ([challenge.protectionSpace.host isEqualToString:MY_PROTECTION_SPACE_HOST])
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
{
if ([challenge previousFailureCount] == 0)
{
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"username"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog (@"failed authentication");
}
}
}
提前致谢。
最佳答案
对于通过 https 进行身份验证,您可以尝试此代码。 更多详情可以从http://as.wiley.com/WileyCDA/WileyTitle/productCd-1119961327,descCd-DOWNLOAD.html下载源代码并查看其中的 iHotelApp。
self.networkQueue = [ASINetworkQueue queue];
[self.networkQueue setMaxConcurrentOperationCount:6];
[self.networkQueue setDelegate:self];
[self.networkQueue go];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:LOGIN_URL]];
[request setUsername:loginName];
[request setPassword:password];
[request setDelegate:self];
[request setDidFinishSelector:@selector(loginDone:)];
[request setDidFailSelector:@selector(loginFailed:)];
[self.networkQueue addOperation:request];
关于iphone - iOS5:通过 https 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11419995/