【发布时间】:2010-10-10 00:23:51
【问题描述】:
如何在 iOS 中以编程方式访问受 .htaccess 或访问控制保护的受密码保护的网站。
使用 NSURLRequest,您可以从 URL 发出 GET 请求。但是,如果 URL 受到保护,您将如何发送用户名密码对。另外,如果它使用标准 MD5 加密方式进行加密呢?
【问题讨论】:
标签: ios
如何在 iOS 中以编程方式访问受 .htaccess 或访问控制保护的受密码保护的网站。
使用 NSURLRequest,您可以从 URL 发出 GET 请求。但是,如果 URL 受到保护,您将如何发送用户名密码对。另外,如果它使用标准 MD5 加密方式进行加密呢?
【问题讨论】:
标签: ios
将这样的内容添加到您的 HTTPConnectionDelegate:
-(void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
【讨论】: