在Objective-c中记录原始HTTP请求/响应

时间:2014-06-26 08:53:51

标签: ios objective-c http httprequest httpresponse

在我的项目中,我正在请求不同的网页,我正在尝试使用NSLog记录RAW HTTP请求/响应,这样我就能看到究竟是什么设置,什么不是。我正在检查原始HTTP请求/响应的方式是设置'Fiddler'并跟踪HTTP。

所以我的问题是,有没有办法在Fiddler跟踪中显示相同的内容到NSLog?

Fiddler 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示例(响应跟踪):

  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

通过做一些研究,我发现我能够得到NSLog的响应,但是我无法将请求送入NSLog。因此,如果有人需要帮助将响应提交到NSLog,那么下面是它的外观和代码示例,以使其工作。唯一缺少的是HTTP / 1.1 200 OK,您可以通过检查响应的状态代码轻松获得。

  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

代码示例以Fiddler跟踪示例(响应跟踪)等格式显示响应。

  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];

所以现在我知道如何格式化和显示原始HTTP响应。有人可以帮我找出是否可以显示原始HTTP请求?

先谢谢。

0 个答案:

没有答案