你可以使用 NSURLConnection 类和 NSMutableURLRequest 来做到这一点。这个想法是让 NSMutableURLRequest 知道您想要使用哪种身份验证方法以及凭据(登录名/密码)。下面的代码应该做到这一点。(您将需要此链接http://cocoadev.com/wiki/BaseSixtyFour中的 base64Encoding 的 NSdata 类别)
self.receivedData = [[NSMutableData alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"login",@"password"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if ([self.receivedData length] >0)
NSString *result = [[NSString alloc] initWithData:downloadedData encoding:NSUTF8StringEncoding];
NSLog(@"The HTML String Is : %@", result);
{