I have developed a iphone 开发者_运维百科application that need to send User name and Password through HTTP authentication request header. can any one guide me or sent the source code for that functionalities.
You'll need to implement the NSURLConnection:didReceiveAuthenticationChallenge: method.
- (void)request:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential = [NSURLCredential
credentialWithUser:@"username"
password:@"password"
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
Edit: Or you could simply send the credentials in the URL in the format http://username:password@www.example.com/
精彩评论