HTTP请求标头 [英] HTTP Request headers

查看:201
本文介绍了HTTP请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好。我一直在使用原始HTTP请求,发现我可以将原始HTTP响应发布到NSLog中,并且我几乎将原始HTTP请求破解为NSLog。我现在只是坚持下去了。

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.

代码示例

   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 和用户代理。

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"?

发出请求

  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


推荐答案

我得出的结论是,不可能获得原始HT使用NSURLRequest在iOS中进行TP请求,您只能获得接受和用户代理。

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".

但是我已经完成了一项工作,以获得我使用php来获取标头的原始HTTP请求。我知道这不是最好的解决方案,但它很有效,能够记录这些信息,而不必像Fiddler或Charles代理那样继续通过代理。

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

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,";
}

这篇关于HTTP请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆