2014-01-07 1 views
-2

인증 보안과 함께 HTTP 요청을 보내려고합니다 .JSON 형식의 응답을 제공합니다. HTTP 요청을 보내려는 자습서 나 샘플 코드를 찾고 있습니다. 응답. 이미 검색했지만 유용한 자습서를 얻지 못했습니다. 제발 도와주세요, 미리 감사드립니다.iPhone 프로그래밍에서 인증 과제로 HTTP 클라이언트 요청을 보내는 방법

+0

당신이 iOS 또는 무엇을 JSON을 구문 분석에 대한 튜토리얼이 필요합니까 링크입니까? –

+0

내 질문을 편집했습니다. – user3007459

+0

http://allseeing-i.com/ASIHTTPRequest/How-to-use – preetam

답변

0

하자 당신이 서비스에서 데이터를 얻을하고 싶은 말은 당신이 지정된 URL 내 서버에서 데이터를 얻을 수 있도록 다음과 같은 방법의 URL을

- (있는 NSString *) getDataFromService : (있는 NSString *) 그래서 응답 문자열에서 strUrl {

NSURL *url = [NSURL URLWithString:strUrl]; 
NSString *response; 


ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 



[request setDelegate:self]; 
[request addBasicAuthenticationHeaderWithUsername:USERNAME 
             andPassword:PASSWORD]; 
[request startSynchronous]; 
NSError *error = [request error]; 

if (!error) { 
    response = [request responseString]; 

} 
else 
{ 

    response=nil; 
} 

return response; 

}

당신은 주어진 URL의 서버에서 응답 데이터를 가지고있다. 여기 ASIHTTPRequest로 명명 GitHub의에서 라이브러리를 donwload 할 필요가 이것에 대한

http://allseeing-i.com/ASIHTTPRequest/How-to-use

0

"NSURLConnectionDataDelegate"를 구현할 수 있습니다. 당신 .H 파일에 .H 파일에서

@interface youClass : NSObject<NSURLConnectionDataDelegate> 

같은 것이 "NSURLConnectionDataDelegate"이 도움이

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace: (NSURLProtectionSpace *)protectionSpace { 
     // NSLog(@"canAuthenticateAgainstProtectionSpace:"); 
     return [protectionSpace.authenticationMethod   isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge { 
    // NSLog(@"didReceiveAuthenticationChallenge:"); 
    [challenge.sender useCredential:[NSURLCredential  credentialForTrust:challenge.protectionSpace.serverTrust]  forAuthenticationChallenge:challenge]; 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // NSLog(@"connectionDidFinishLoading"); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    // NSLog(@"Recived data!!!"); 
} 

희망의 대리자 메서드를 구현 않습니다. :)