Aug 06
[iPhone] NSURLRequest로 받은 response에서 Http header를 가져오기 – Accessing HTTP Headers From An NSURLRequest
iPhone에서 http request/response는 아주 쉽고 여러 가지 용도로 잘 사용할 수 있게 제공되어 있다.
Mobile Orchard에 보니 response 받는 Http의 header를 가져오는 것에 대해서 포스트가 있어서 포스팅한다.
답은 NSHTTPURLRespones의 allHeaderFields method를 이용하는 것이다.
Synchronous request의 코드는 아래와 같고,
NSURL *url = [NSURL URLWithString:@"http://www.mobileorchard.com"]; NSURLRequest *request = [NSURLRequest requestWithURL: url]; NSHTTPURLResponse *response; [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: nil]; NSDictionary *dictionary = [response allHeaderFields]; NSLog([dictionary description]);
Asynchronous request는 connection:didReceiveResponse:를 이용해서 아래와 같다.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
}
Ref: Accessing HTTP Headers From An NSURLRequest
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
