YaBCK
YaBCK

Reputation: 3029

HTTP Request headers

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

Upvotes: 3

Views: 8951

Answers (2)

Dimitri Bouniol
Dimitri Bouniol

Reputation: 766

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.

Upvotes: 0

YaBCK
YaBCK

Reputation: 3029

I've come to the conclusion that it's not possible to get the raw HTTP request in iOS with NSURLRequest, you're only able to get "Accept" and "User-Agent".

However i've done a work around, to get the raw HTTP request i've used php to get the headers. I know it's not the best solution but it works and it's great being able to log this information without having to keep going through a proxy like Fiddler or Charles proxy.

iOS code

NSURL *PHPURL = ([NSURL URLWithString:[NSString stringWithFormat:@"http://office.isosec.co.uk/Audit/testRequest.php?"]]);
request = [[NSMutableURLRequest alloc] initWithURL:PHPURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod: @"GET"];
[request setHTTPShouldHandleCookies:YES];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

PHPResponse = [NSMutableData data];

if( connection == nil ) {
    [ViewController remoteLog:[NSString stringWithFormat:@"PHPRequest Connection Failed"] withlevel:SPECIAL_LOG];
} else {
    [ViewController remoteLog:[NSString stringWithFormat:@"PHPRequest Connection Started"] withlevel:SPECIAL_LOG];
    [connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
    [connection start];
}

PHP code

foreach (getallheaders() as $name => $value) {
    echo "$name: $value,";
}

Upvotes: 1

Related Questions