HTTP Request headers



Answers

It is possible to get the URL Request headers that actually did get sent to the server by requesting the task's currentRequest, which will give you more than just Accept and User-Agent, at least in my limited testing:

__block NSURLSessionDataTask *task = nil; // Use __block because we need to reference this particular pointer, not its current address

task = [session dataTaskWithRequest:originalRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSURLRequest *request = task.currentRequest;

    NSLog(@"Request HTTP Headers: %@", request.allHTTPHeaderFields);
}];

[task resume];

Edit: Unfortunately, it seems that it doesn't give you all the headers, just a few more like Accept-Encoding and Accept-Language.

Question

Okay. I've been working with raw HTTP Request and found out that I can post the Raw HTTP Response into NSLog and i've nearly cracked the raw HTTP Request into NSLog. I'm just abit stuck now.

Code Example

   NSString *CurrentWebURL = webView.request.URL.absoluteString;
   NSURLSession *session= [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
   [[session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:CurrentWebURL]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
   NSDictionary *requestdictionary = [request allHTTPHeaderFields];
   NSLog(@"----shouldStartLoadWithRequest Raw HTTPRequest Start ----");
   for (NSString *Object in [requestdictionary allKeys]) {
         NSLog(@"%@: %@",Object, [requestdictionary objectForKey:Object]);
   }
   NSLog(@"----shouldStartLoadWithRequest Raw HTTPRequest Finish ----"] withlevel:SPECIAL_LOG);

   }]resume];

Raw Request:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_6 like Mac OS X)  AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B651

It should be showing me "Get:URL", "HOST", "Connection: Keep-alive", "Accept-Encoding", "Accept", "Cookie", "Connection", "Accept-Language" and "User-Agent".

My question is why is it only showing the "Accept" and "User-Agent"?

Making Request:

  NSURL *url = [NSURL URLWithString:PortalURL];

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
  [request setHTTPShouldHandleCookies:YES];
  [request setHTTPMethod: @"GET"];

Fiddler Request Trace (Without any custom headers):

  GET http://URL/ HTTP/1.1
  Host: Host.co.uk
  Connection: keep-alive
  Accept-Encoding: gzip, deflate
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Cookie: IsosecCookie=Chris%20Beckett
  Connection: keep-alive
  Accept-Language: en-gb
  User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B651

Logging Request Headers in NSLog:

  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B651





Links



Tags