【问题标题】:GET succeeds with NSURLSession but fails with AFHTTPSessionManagerGET 使用 NSURLSession 成功,但使用 AFHTTPSessionManager 失败
【发布时间】:2016-08-18 02:41:38
【问题描述】:

我无法使用预签名的 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()

【问题讨论】:

    标签: swift afnetworking afnetworking-3


    【解决方案1】:

    AFNetworking 由于内容类型问题而未能请求。在我将 S3 文件的内容类型从 binary/octet 更改为 image/jpeg 后,setImageWithURL 起作用了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2016-07-15
      相关资源
      最近更新 更多