【问题标题】:Getting "Location" header from NSHTTPURLResponse从 NSHTTPURLResponse 获取“位置”标头
【发布时间】:2010-03-25 08:21:12
【问题描述】:

根本无法从响应中获取“位置”标头。 Wireshark 说我有一个:

地点: 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”的东西。

【问题讨论】:

    标签: objective-c iphone-sdk-3.0


    【解决方案1】:

    NSHTTPURLResponseNSURLResponse 的子类,因此您可以向它索取URL。这将返回一个 NSURL 对象,您可以从中获取 URL 组件,例如查询、参数字符串或片段。在您的情况下,您需要片段组件:

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

    【讨论】:

    • 非常感谢!它有帮助! =)
    【解决方案2】:

    认为用如何在 Swift 中访问 Location 标头的示例来补充@Rob Keniger 的答案可能会有所帮助。如上所示,“Location”是一个 HTTP 响应头字段,所以在 Swift 中可以这样做:

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

    其中response 的类型为NSHTTPURLResponse。见NSHTTPURLResponse Class Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-07
      相关资源
      最近更新 更多