GET 使用 NSURLSession 成功,但使用 AFHTTPSessionManager 失败

GET succeeds with NSURLSession but fails with AFHTTPSessionManager

我在使用预签名 url 和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequestNSURLSession 获取图像,但是当我使用 AFHTTPSessionManager 和相同的 NSMutableURLRequest 时出现 403 错误。 (我实际上只想通过调用 setImageWithURLRequest 来加载图像,这也会给我一个 403 错误,但我编写了这段使用 AFHTTPSessionManager 的代码来尝试调试问题。)我做错了什么 AFNetworking?

let request = NSMutableURLRequest()
request.HTTPMethod = "GET"
request.URL = coverPhotoUrl // A presigned url to an image on S3

// The operation succeeds if I uncomment the NSURLSession line
// and comment out the AFHTTPSessionManager line.
//NSURLSession.sharedSession().dataTaskWithRequest(request) {
AFHTTPSessionManager().dataTaskWithRequest(request) {
    date, response, error in
    if let error = error {
        Log.error?.message("request failed: \(error.localizedDescription)")
    } else {
        Log.debug?.message("request succeeded.")
    }
}
.resume()

AFNetworking 由于内容类型问题导致请求失败。 setImageWithURL 在我将 S3 文件的内容类型从 binary/octet 更改为 image/jpeg 后工作。

让 S3 设置内容类型也很困难。如果它对其他人有用,S3 会忽略我的 PUT 请求中的内容类型,直到我指定了内容长度。尽管实际文件要长得多,但在生成预签名 URL 时我只是使用了 1 的虚拟长度。