요청 헤더에 기본 인증을 추가하려고합니다. 아래의 코드는 컴파일되고 런타임 오류가 발생하지 않습니다. 그러나 서버 측에서는 헤더에 "Authorization"이 전혀 표시되지 않습니다.NSMutableURLRequest의 기본 인증이 작동하지 않습니다.
나는 didReceiveAuthenticationChallenge 메서드를 구현할 수 있었지만 제대로 작동했지만 왜 이런 식으로해야하는지 이해하지 못합니다. 모든 요청에 기본 인증을 항상 추가하기 만하면됩니다.
저는 ASIHTTPRequest 사용에 관심이 없습니다.
도움 주셔서 감사합니다.
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:8000/MyWebService"];
self.userName = @"myusername";
self.password = @"mypassword";
NSMutableString *credentials = [[NSMutableString alloc] initWithFormat:@"%@:%@", userName, password];
NSString *encodedCredentials = [[credentials dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString *authHeader = [NSString stringWithFormat:@"Basic %@", encodedCredentials];
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
[req addValue:authHeader forHTTPHeaderField:@"Authorization"];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
if (self.urlConnection) {
self.receivedData = [NSMutableData data];
}
else {
errorLabel.text = @"Error connecting to the server";
}