好。我一直在处理原始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
它应该显示我"获取:URL","主机","连接:保持活动","接受编码&#34 ;,"接受"," Cookie","连接","接受语言"和"用户代理"。
我的问题是为什么它只显示"接受"和"用户代理"?
提出请求:
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
答案 0 :(得分:1)
我得出的结论是,使用NSURLRequest无法在iOS中获取原始HTTP请求,您只能获得"接受"和"用户代理"。
但是我已经完成了一项工作,为了获得原始的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,";
}
答案 1 :(得分:0)
通过请求任务currentRequest
,可以获得实际上已经发送到服务器的URL请求标头,这将为您提供的不仅仅是Accept
和{{1至少在我的有限测试中:
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
更多。