我必须通过调用 api /api/places来建立与服务器http://sand.com的连接。我将 NSMutableURLRequest 对象用于上述 url 和 Authorization 标头。建立了几次连接,但我没有收到数据,但现在根本没有建立连接,而是多次调用了 didReceiveAuthenticationChallenge。(授权标头设置值是用户名:base64 格式的密码)我哪里出错了?
NSString *urlString =
[NSString stringWithFormat:@"http://sand.com/api/places",hostURL];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:300.0];
[theRequest setValue:@"Basic c3VyYWJoaV9taXNocmE6bWlzaHJhaml2ZQ==" forHTTPHeaderField:@"Authorization"];
[theRequest setHTTPMethod:@"GET"];
NSURLconnections *nuc = [NSURLconnections new];
nuc.delegate = self;
[nuc connectToRequest:theRequest];
- (void)connectToRequest:(NSURLRequest*)theRequest
{
urlConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (urlConnection)
{
responseData=[NSMutableData data];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0)
{
NikeAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
username = appDelegate.username; password = appDelegate.password;
NSURLCredential *cred = [[NSURLCredential alloc]
initWithUser:username
password:password
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
}
}