0

How do I get http headers by using sendAsynchronousRequest? Notice that the target URL of the URLRequest only sets headers. When I use sendSynchronousRequest, I can get the http headers by using the NSURLHTTPResponse object. However, in sendAsynchronousRequest, there is only a NSURLResponse object. I have used the following cast strategy to convert NSURLResponse to NSURLHTTPResponse in sendAsynchronousRequest method. This however does not work, blockinkg the main thread without generating any error or exception.\

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {

    NSHTTPURLResponse *hResponse = (NSHTTPURLResponse*) response;
             if ([hResponse respondsToSelector:@selector(allHeaderFields)]) {
                 NSDictionary *dictionary = [hResponse allHeaderFields];
                 _myHeader = [dictionary objectForKey:@"myHeader"];
             }
 }

If I don't use the above code, there is no blocking. I have also notice that the NSData returned by sendAsynchronousRequest hás zero length. Please help

User1983
  • 1
  • 1
  • Have you tried logging response, data and error in the completionHandler? Or setting a breakpoint there and checking what you receive and/or at what point things block? – jcaron Jun 26 '14 at 23:17
  • @jcaron I think you've seen the post without the edit. The scope of the completionHandler is starts at the first "{". The lenght of Data is zero. I will check if the NSURLResponse reference ("response") is nil. I guess it should be since the cast already blocks the system – User1983 Jun 26 '14 at 23:25
  • Check out http://stackoverflow.com/questions/9270447/how-to-use-sendasynchronousrequestqueuecompletionhandler for all the tests you should perform. – jcaron Jun 26 '14 at 23:28

0 Answers0