我必须通过调用api / api / places建立与服务器http://sand.com的连接。 我在上面的url和Authorization标头中使用了NSMutableURLRequest对象。 几次连接建立但我没有收到数据,但现在Connection根本没有建立,而是多次调用didReceiveAuthenticationChallenge。 (Authorization标头setvalue是用户名: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];
}
}