HTTP Request headers

偶尔善良 提交于 2020-01-04 04:25:26

问题


Okay. I've been working with raw HTTP Request and found out that I can post the Raw HTTP Response into NSLog and i've nearly cracked the raw HTTP Request into NSLog. I'm just abit stuck now.

Code Example

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

Raw Request:

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

It should be showing me "Get:URL", "HOST", "Connection: Keep-alive", "Accept-Encoding", "Accept", "Cookie", "Connection", "Accept-Language" and "User-Agent".

My question is why is it only showing the "Accept" and "User-Agent"?

Making Request:

  NSURL *url = [NSURL URLWithString:PortalURL];

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

Fiddler Request Trace (Without any custom headers):

  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

Logging Request Headers in 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

回答1:


I've come to the conclusion that it's not possible to get the raw HTTP request in iOS with NSURLRequest, you're only able to get "Accept" and "User-Agent".

However i've done a work around, to get the raw HTTP request i've used php to get the headers. I know it's not the best solution but it works and it's great being able to log this information without having to keep going through a proxy like Fiddler or Charles proxy.

iOS code

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 code

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



回答2:


It is possible to get the URL Request headers that actually did get sent to the server by requesting the task's currentRequest, which will give you more than just Accept and User-Agent, at least in my limited testing:

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

Edit: Unfortunately, it seems that it doesn't give you all the headers, just a few more like Accept-Encoding and Accept-Language.



来源:https://stackoverflow.com/questions/24431225/http-request-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!