【问题标题】:How do I get the default user-agent string in an NSURLConnection?如何在 NSURLConnection 中获取默认的用户代理字符串?
【发布时间】:2011-04-15 15:52:05
【问题描述】:

我想将文本附加到NSURLConnection 中的默认用户代理标头。我知道如何change the user-agent of the NSURLConnection,但我看不到如何获取默认用户代理。我尝试了以下方法:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
userAgent = [userAgent stringByAppendingString:extraUserAgentInfo];
[request addValue:userAgent forHTTPHeaderField:@"User-Agent"];

这不起作用,因为 userAgent 从 valueForHTTPHeaderField: 调用返回 nil

【问题讨论】:

  • 你试过“User_Agent”吗?

标签: iphone cocoa-touch nsurlconnection


【解决方案1】:

默认 User-Agent 的一部分是您的 AppName 和版本:

我的用户代理是:

User-Agent  foo-bar/1.0 CFNetwork/609.1.4 Darwin/12.4.0

foo-bar[[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleNameKey] 的值

1.0[[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleVersionKey] 的值

【讨论】:

    【解决方案2】:

    iCab 有一个博客条目Changing the headers for UIWebKit HTTP requests。他建议使用进程调用"Method Swizzling” 来获得您想要的。不过有传言说Method swizzling cause for 4.0 rejection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      相关资源
      最近更新 更多