【发布时间】:2014-06-26 12:59:38
【问题描述】:
好的。我一直在使用原始 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"];
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 为您添加了请求对象,我不想设置任何自定义标头字段。
标签: ios objective-c http-headers httprequest