HTTP请求标头

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

提琴手请求跟踪(没有任何自定义标题):

  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中logging请求标题:

  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 

我得出的结论是,在NSURLRequest中无法获得原始的HTTP请求,你只能得到“接受”和“用户代理”。

不过,我已经做了一个工作,获得原始的HTTP请求,我用PHP来获取标题。 我知道这不是最好的解决scheme,但它的工作原理,能够logging这些信息,而无需继续通过像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,"; } 

可以通过请求任务的currentRequest来获取实际上发送到服务器的URL请求头,这将至less在我的有限testing中给予您不仅仅是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