NSUrlRequest:应用程序可以在哪里找到 HTTP 请求的默认标头?

发布于 2024-11-01 22:20:27 字数 687 浏览 1 评论 0原文

有谁知道 iOS 应用程序可以在哪里看到 NSUrlRequest 为 HTTP 请求设置的默认标头?

只需使用 "http://.." NSURL 创建 NSUrlRequest ,然后询问:[request allHTTPHeaderFields] 返回一本空字典。但是我知道例如“Accept-Encoding” 设置为“gzip”。所以我想获取所有这些字段并在 HTTP 请求演示中显示它们。

我还尝试 swizzle [NSMutableURLRequest setValue:forHTTPHeaderField:],但底层 API(NSURLRequest 或 NSURLConnection)似乎没有使用它来设置我正在寻找的默认字段。

我只是制作一个简单的 iOS 演示,它显示 HTTP 请求和响应信息,因此它是用于此目的的公共 API 还是私有 API 并不重要。

Does anybody know where an iOS app can see the default headers that NSUrlRequest sets for an HTTP request?

Just creating NSUrlRequest with "http://.." NSURL and then asking: [request allHTTPHeaderFields] returns an empty dictionary. But I know that for example "Accept-Encoding" is set to "gzip". So I want to get all that fields and show them in a HTTP request demo.

I've also tried to swizzle [NSMutableURLRequest setValue:forHTTPHeaderField:], but it seems that it is not used by underlying API (NSURLRequest or NSURLConnection) to set those default fields I'm hunting for.

I'm making just a simple iOS demo which shows HTTP request and response information, so it doesn't really matters if it will be a public or private API used for that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-11-08 22:20:28

嗯...也许您可能想

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

在自定义 nsurlconnection 类中的方法内尝试。尽管文档提到了有关重定向的一些内容,但这确实值得研究。

hmm... maybe you might want to try within

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

method in your custom nsurlconnection class. although the documentation mentions something about redirects, this is certainly worth looking into.

傲性难收 2024-11-08 22:20:28

这可能有点矫枉过正,但基于 Matt Gallagher 的博客文章 我创建了更简单的本地 HTTP 侦听器,并向其发送了一个单独的 [mutableCopy]ed 请求,以便能够读取和输出该请求具有的所有 HTTP 标头。

更好的解决方案必须是设置一个本地 HTTP 请求捕获器,我认为它看起来一定更好,但对于简单演示的范围来说,只是显示所有发送/接收的标头,这个解决方案是可以的。

That may be an overkill, but based on Matt Gallagher's blog post I've created even more simpler local HTTP listener and sent a separate [mutableCopy]ed request to it to be able to read and output all HTTP headers that this request has.

Better solution must be to setup a local HTTP request catcher, it must look nicer I think, but for the scope of simple demo just to show all sent/received headers this solution is OK.

救赎№ 2024-11-08 22:20:27

你的应用程序不能。这一切都在 CFNetwork - 与 HTTP 通信中完成服务器。我相信它只是添加 NSURLRequest

默认值为:

  • USER-AGENT“AppName - Eng/1.0 CFNetwork/485.13.9 Darwin/10.7.0”
  • ACCEPT“*/*”
  • ACCEPT-LANGUAGE “en-us”
  • 接受编码“gzip,deflate”
  • 连接“保持活动”

Your app cannot. It's done all down in CFNetwork - Communicating with HTTP Servers. I believe it just adds missing header values not supplied by NSURLRequest.

The defaults are:

  • USER-AGENT "AppName - Eng/1.0 CFNetwork/485.13.9 Darwin/10.7.0"
  • ACCEPT "*/*"
  • ACCEPT-LANGUAGE "en-us"
  • ACCEPT-ENCODING "gzip, deflate"
  • CONNECTION "keep-alive"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文