我在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");
}
}
}
提前致谢。
答案 0 :(得分:2)
要通过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];