【问题标题】:HTTP Request headersHTTP 请求标头
【发布时间】:2014-06-26 12:59:38
【问题描述】:

好的。我一直在使用原始 HTTP 请求,发现我可以将原始 HTTP 响应发布到 NSLog 中,并且我几乎将原始 HTTP 请求破解到 NSLog 中。我现在有点卡住了。

代码示例

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

原始请求:

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

它应该显示“Get:URL”、“HOST”、“Connection: Keep-alive”、“Accept-Encoding”、“Accept”、“Cookie”、“Connection”、“Accept-Language”和“用户代理”。

我的问题是为什么它只显示“接受”和“用户代理”?

提出请求:

  NSURL *url = [NSURL URLWithString:PortalURL];

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

Fiddler 请求跟踪(没有任何自定义标头):

  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

在 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

【问题讨论】:

  • 您能说明您是如何创建request 对象并设置标题字段的吗?
  • @Stonz2 为您添加了请求对象,我不想设置任何自定义标头字段。

标签: ios objective-c http-headers httprequest


【解决方案1】:

我得出的结论是,在 iOS 中使用 NSURLRequest 无法获取原始 HTTP 请求,您只能获取“Accept”和“User-Agent”。

但是我已经解决了问题,为了获取原始 HTTP 请求,我使用 php 来获取标头。我知道这不是最好的解决方案,但它确实有效,并且能够记录这些信息而无需继续通过像 Fiddler 或 Charles 代理这样的代理非常棒。

iOS 代码

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 代码

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

【讨论】:

    【解决方案2】:

    可以通过请求任务的currentRequest 来获取实际发送到服务器的 URL 请求标头,这将为您提供的不仅仅是 Accept 和 User-Agent,至少在我有限的测试中:

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

    编辑:不幸的是,它似乎并没有给你所有的标题,只是像Accept-Encoding和Accept-Language这样的一些。

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 2015-07-13
      • 2019-01-08
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2016-06-29
      相关资源
      最近更新 更多