防止NSURLSession默认HTTP标头 [英] Preventing NSURLSession default HTTP headers

查看:134
本文介绍了防止NSURLSession默认HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为服务器创建一个非常具体的HTTP请求(即定义完全的HTTP标头集),但是 NSURLSession 保持帮助插入一堆HTTP标头,如接受接受语言接受编码

I'm trying to craft a very specific HTTP request to a server (ie. defining the exact set of HTTP headers), but NSURLSession keeps "helpfully" inserting a bunch of HTTP headers like Accept, Accept-Language and Accept-Encoding.

考虑以下操场(Swift 2.x)向服务发送请求,该服务只回显已发送的HTTP标头:

Consider the following playground (Swift 2.x) which sends a request to a service that just echos the HTTP headers that were sent:

import Foundation
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

let url = NSURL(string: "http://httpbin.org/headers")!
let request = NSMutableURLRequest(URL: url, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 30000)
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let session = NSURLSession(configuration: configuration)

let task = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) in
    print(NSString(data: data!, encoding: NSUTF8StringEncoding))
    XCPlaygroundPage.currentPage.finishExecution()
}
task.resume()

您可以看到有三个 Accept 标头被发送。我该如何防止这种情况?

You can see that there are three Accept headers being sent. How can I prevent that?

我尝试使用 request.setValue设置标头(nil,forHTTPHeaderField:Accept-Language)但是会被忽略。尝试将其设置为,但没有好处。我也尝试在 NSURLSessionConfiguration 上操纵 HTTPAdditionalHeaders 属性,但没有爱。

I've tried setting the header using request.setValue(nil, forHTTPHeaderField: "Accept-Language") but that gets ignored. Tried setting it to "", but no good. I've also tried manipulating the HTTPAdditionalHeaders property on NSURLSessionConfiguration, but no love.

如何获得 NSURLSession 不是那么有用?

How do I get NSURLSession to not be quite so helpful?

推荐答案

我怀疑你所要求的是可能的。 NSURLSession(和NSURLConnection)会自动提供多个标题,这就是其中之一。

I doubt what you're asking for is possible. NSURLSession (and NSURLConnection) automatically provide a number of headers, and that's one of them.

也没有合理的理由删除它们。自最初的HTTP / 0.9规范以来,所有这三个标题都已成为规范的一部分( https ://www.w3.org/Protocols/HTTP/HTRQ_Headers.html )。对于没有正确处理它们或完全忽略它们的服务器,绝对没有任何借口。

There's also no valid reason to remove them. All three of those headers have been part of the spec since the original HTTP/0.9 spec (https://www.w3.org/Protocols/HTTP/HTRQ_Headers.html). There's absolutely no excuse for any server not either handling those correctly or ignoring them outright.

如上所述,如果你为这些字段提供了错误的值(默认情况下可能是错的),服务器可能会拒绝给你结果。要解决这个问题,首先要弄清楚服务器实际提供的数据类型,并在Accept标头中指定该值而不是默认值。

With that said, if you provide the wrong value for those fields (and the default may be wrong), the server may refuse to give you results. To solve that problem, first figure out what type of data the server is actually going to provide, and specify that value in the Accept header instead of the default.

例如,您可以将Accept设置为application / json或其他变体之一(什么是正确的JSON内容类型?)如果您期望JSON数据。

For example, you might set Accept to "application/json" or one of the other variants (What is the correct JSON content type?) if you're expecting JSON data.

那就是说,如果你真的必须避免发送这些标头,您可以随时打开套接字,手动构建请求并发送它。假设服务器不需要分块编码,那么很容易做到。 (然而,分块编码是一个惊人的比例,所以如果你的服务器发送回来,你几乎不得不求助于将libcurl添加到你的项目中并使用它来代替发出请求。)

That said, if you really must avoid having those headers sent, you can always open a socket, construct the request manually, and send it. Assuming the server doesn't require chunked encoding, it is pretty easy to do. (Chunked encoding, however, is a horror of Herculean proportions, so if your server sends that back, you'll pretty much have to resort to adding libcurl into your project and using that to make requests instead.)

这篇关于防止NSURLSession默认HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆