android - HTTP 上传返回 invalid put policy encoding 错误
PHPz
PHPz 2017-04-17 11:48:29
0
0
839

总的来说

  1. 服务端使用七牛官方 Ruby SDK 生成的 uptoken
  2. Android 客户端使用 Retrofit 构建上传,失败:invalid put policy encoding
  3. 自己构建 Multipart 请求体,失败同上
  4. 已自己检查 upload token,以 : 为分割的第一段确为 Access Key 的 base64 编码;第三段确为 put policy 的 json 的 base64 编码(如果这个都能错就是你们 Ruby SDK 的问题)

生成 upload token

def upload_token
  put_policy = Qiniu::Auth::PutPolicy.new 'xxxxxx'
  put_policy.fsize_limit = "20971520"
  put_policy.mime_limit = "image/*;video/*"
  token = Qiniu::Auth.generate_uptoken(put_policy)
  render json: {token: token}, status: :created
end

使用 Retrofit 请求

@Multipart
@POST("/")
void upload(@Part("file") CountingTypedStream stream,
            @Part("token") TypedString token,
            Callback<UploadResult> callback);

产生的请求体如下(省略 file part。SF 你又擅自着色我的代码了!):

--667fdbb3-301f-4f25-ab8d-acc80ca14192
Content-Disposition: form-data; name="token"
Content-Type: text/plain; charset=UTF-8
Content-Length: 202
Content-Transfer-Encoding: binary
Y0这是我的AK无误t5:1o1Ic这里也没问题42UVEo=:eyJzY这一串也没问题KiJ9
--667fdbb3-301f-4f25-ab8d-acc80ca14192--

返回给我的东西(取自日志):

07-24 22:59:43.266 D/Retrofit﹕ <--- HTTP 401 http://upload.qiniu.com/ (13126ms)
07-24 22:59:43.266 D/Retrofit﹕ : HTTP/1.1 401 Unauthorized
07-24 22:59:43.266 D/Retrofit﹕ Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size
07-24 22:59:43.266 D/Retrofit﹕ Access-Control-Allow-Methods: OPTIONS, HEAD, POST
07-24 22:59:43.266 D/Retrofit﹕ Access-Control-Allow-Origin: *
07-24 22:59:43.266 D/Retrofit﹕ Cache-Control: no-store, no-cache, must-revalidate
07-24 22:59:43.266 D/Retrofit﹕ Connection: close
07-24 22:59:43.266 D/Retrofit﹕ Content-Length: 39
07-24 22:59:43.266 D/Retrofit﹕ Content-Type: application/json
07-24 22:59:43.266 D/Retrofit﹕ Date: Thu, 24 Jul 2014 14:59:43 GMT
07-24 22:59:43.266 D/Retrofit﹕ Pragma: no-cache
07-24 22:59:43.266 D/Retrofit﹕ Server: nginx/1.4.4
07-24 22:59:43.266 D/Retrofit﹕ X-Android-Received-Millis: 1406213983252
07-24 22:59:43.266 D/Retrofit﹕ X-Android-Response-Source: NETWORK 401
07-24 22:59:43.266 D/Retrofit﹕ X-Android-Selected-Transport: http/1.1
07-24 22:59:43.266 D/Retrofit﹕ X-Android-Sent-Millis: 1406213970562
07-24 22:59:43.266 D/Retrofit﹕ X-Content-Type-Options: nosniff
07-24 22:59:43.266 D/Retrofit﹕ X-Log: UP:17/401
07-24 22:59:43.266 D/Retrofit﹕ X-Reqid: JUAAAFZBtNkq4IMT
07-24 22:59:43.266 D/Retrofit﹕ X-Via: 1.1 gzqxg204:1 (Cdn Cache Server V2.0)
07-24 22:59:43.274 D/Retrofit﹕ {"error":"invalid put policy encoding"}
07-24 22:59:43.274 D/Retrofit﹕ <--- END HTTP (39-byte body)

自己手动拼接构建的请求体

--4da8f9b3-4e83-483b-b206-96faf1c4574d
Content-Disposition: form-data; name="token"

我是PutPolicy肯定没问题啦啦啦啦啦啦
--4da8f9b3-4e83-483b-b206-96faf1c4574d
Content-Disposition: form-data; name="file"; filename="stream_file"
Content-Type: application/octet-stream

文件内容
--4da8f9b3-4e83-483b-b206-96faf1c4574d--

完全按照 http://developer.qiniu.com/docs/v6/api/reference/up/upload.html 的格式构建,Header 不多不少。

PHPz
PHPz

学习是最好的投资!

모든 응답(0)
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿