'Logging raw HTTP request/response in Objective-c

In my project I am requesting different web pages and I am trying to log the RAW HTTP request/response with NSLog so I can see what exactly is being set and what isn't. The way at the moment I am checking the raw HTTP request/response is by setting up 'Fiddler' and doing a trace of the HTTP.

Is there a way of displaying the same content which is inside the Fiddler trace to a NSLog?

Fiddler Trace Example (Request Trace):

  GET https://spine.isosec.co.uk/MIA/Portal/0.2.6/match.manifest HTTP/1.1
  Host: spine.isosec.co.uk
  Proxy-Connection: keep-alive
  Accept-Encoding: gzip, deflate
  Accept: */*
  Cookie: Origin=http%3A%2F%2Fspine.isosec.co.uk%2F
  Accept-Language: en-gb
  Connection: keep-alive
  Cache-Control: max-age=0
  User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML,     like Gecko) Mobile/11B651

Fiddler Trace Example (Response Trace):

  HTTP/1.1 200 OK
  Server: nginx/1.4.7
  Date: Thu, 26 Jun 2014 11:20:31 GMT
  Content-Type: text/html
  Content-Length: 1092
  Connection: keep-alive
  Last-Modified: Tue, 25 Mar 2014 09:38:01 GMT
  ETag: "cbf-4f56b1ba70dd8-gzip"
  Accept-Ranges: bytes
  Vary: Accept-Encoding
  Content-Encoding: gzip
  Cache-Control: max-age=7200, must-revalidate

With doing some research, i've found out that i'm able to get the response into NSLog, however i'm not able to get the Request into NSLog. So if anybody needs help getting the response into NSLog then below is how it'll look and the code example to getting it working. The only thing that is missing from this is the HTTP/1.1 200 OK which you can easily get from checking the Status Code for the response.

  Cache-Control:max-age=7200, must-revalidate
  Accept-Ranges:bytes
  Content-Encoding:gzip
  Date:Thu, 26 Jun 2014 11:20:31 GMT
  Vary:Accept-Encoding
  Connection:keep-alive
  Content-Length:1092
  Content-Type:text/html
  Server:nginx/1.4.7
  Etag:"cbf-4f56b1ba70dd8-gzip"
  Last-Modified:Tue, 25 Mar 2014 09:38:01 GMT

Code Example to show the Response in the format like Fiddler Trace Example (Response Trace).

  NSURLSession *session= [NSURLSession sessionWithConfiguration:                                   
  [NSURLSessionConfiguration defaultSessionConfiguration]];
  [[session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURLURLWithString:CurrentWebURL]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
      NSDictionary *responsedictionary =  [(NSHTTPURLResponse*)response allHeaderFields];
      for (NSString *Object in [responsedictionary allKeys]) {
          NSLog(@"shouldStartLoadWithRequest HTTPResponseMethod: %@:%@",Object,[responsedictionary objectForKey:Object]);
      }

  }]resume];

So now I know how to format and show the raw HTTP Response. Could someone please help me with finding out if it's possible to show the raw HTTP Request?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source