2010-03-25 134 views
4

无法从响应获取“位置”标题。 Wireshark的说,我有一个:从NSHTTPURLResponse获取“位置”标题

地点: HTTP://*/index.html#0; SID = 865a84f0212a3a35d8e9d5f68398e535

NSHTTPURLResponse *hr = (NSHTTPURLResponse*)response; 
NSDictionary *dict = [hr allHeaderFields];   
NSLog(@"HEADERS : %@",[dict description]); 

产生以下:

HEADERS : { 
    Connection = "keep-alive"; 
    "Content-Encoding" = gzip; 
    "Content-Type" = "text/html"; 
    Date = "Thu, 25 Mar 2010 08:12:08 GMT"; 
    "Last-Modified" = "Sat, 29 Nov 2008 15:50:54 GMT"; 
    Server = "nginx/0.7.59"; 
    "Transfer-Encoding" = Identity; 
} 

任何地方都没有位置。如何得到它?我需要这个“sid”的东西。

回答

5

NSHTTPURLResponseNSURLResponse的子类,所以你可以问它的URL。这会返回一个NSURL对象,您可以从中获取URL组件,如查询,参数字符串或片段。在你的情况你想要的片段部分:

NSURL* url = [response URL]; 
NSString* fragment = [url fragment]; 
//fragment should be: 0;sid=865a84f0212a3a35d8e9d5f68398e535 
+1

非常感谢你!它帮助! =) – aspcartman 2010-03-26 10:51:28

1

想这可能是有益的补充@Rob Keniger的回答与如何访问位置头在斯威夫特的例子。如上所示,“位置”是HTTP响应报头字段,因此在夫特这是可以做到如下:

if let location = response.allHeaderFields["Location"] as? String 
{ 
    NSLog("Location \(location)") 
} 

其中responseNSHTTPURLResponse类型。见NSHTTPURLResponse Class Reference