2011-11-29 4 views
3

요청 헤더에 기본 인증을 추가하려고합니다. 아래의 코드는 컴파일되고 런타임 오류가 발생하지 않습니다. 그러나 서버 측에서는 헤더에 "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"; 
} 

답변

3

didReceiveAuthenticationChallenge는 아이폰 OS에서 작동하는 방식과 그것을 할 수있는 가장 쉬운 방법입니다 :

이 아래에있는 내 코드입니다. 모든 요청과 함께 제출됩니다 (챌린지 포함). 당신이 듣고 싶지 않은 것 같아요 =)

다른 튜토리얼을 사용해 보았습니다. 이전에이 번호를 사용하여 인증했습니다.

Basic access Auth, iOS

관련 문제