HTTP请求头部

3

好的。我一直在处理原始的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"和"User-Agent"。

我的问题是为什么只显示"Accept"和"User-Agent"?

发送请求:

  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
@Stonz2,我已经为您添加了请求对象,并且不想设置任何自定义标头字段。 - YaBCK
2个回答

1
我得出结论,在iOS中使用NSURLRequest无法获取原始HTTP请求,只能获取“Accept”和“User-Agent”。
但是,我已经想到了一个解决方法,通过使用PHP获取标头来获取原始HTTP请求。我知道这不是最好的解决方案,但它有效,并且可以记录此信息,而无需继续通过代理(如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,";
}

0

可以通过请求任务的currentRequest来获取实际发送到服务器的URL请求头,这将为您提供更多信息,至少在我的有限测试中不仅仅是AcceptUser-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-EncodingAccept-Language这样的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接