2014-06-26 304 views
3

好的。我一直在使用原始HTTP請求,發現我可以將原始HTTP響應發佈到NSLog中,並且我幾乎將原始HTTP請求分解爲NSLog。我現在只是升遷卡住了。HTTP請求標頭

代碼示例

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」, 「HOST」, 「連接:保持活動」, 「接受編碼」,「接受」,「Cookie」,「連接」,「接受語言」和「用戶代理」。

我的問題是爲什麼它只顯示「接受」和「用戶代理」?

製作要求:

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日誌請求頭:

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

你能告訴你如何創建你的'request'對象並設置你的頭域嗎? – Stonz2

+0

@ Stonz2爲您添加了請求對象,我不想設置任何自定義標題字段。 –

回答

1

我得出的結論是,在NSURLRequest中無法獲得iOS中的原始HTTP請求,只能獲得「Accept」和「User-Agent」。

但是我已經做了一個解決方法,以獲得原始的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,"; 
} 
0

有可能通過請求得到實際上沒有被髮送到服務器的URL請求頭任務的currentRequest,這至少在我的有限測試中會給你不止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]; 

編輯:不幸的是,它似乎並沒有給你所有的標題,只是幾個更像01​​和Accept-Language