如何从HTTP CORS获取文件信息?

时间:2019-03-31 15:17:06

标签: java kotlin http-headers okhttp3

我正在客户端使用OkHttp库获取文件信息。我正在尝试使用以下代码从Google云端硬盘中获取它:

val url = "https://doc-0g-0c-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/uqi0f95jaa3cp9t78aqcmfg352flft7d/1554019200000/16042636045718966916/*/1vjNTRlUeMXFx9_H3IE7jw5yBKV0F3bIO?e=download"
val request = Request.Builder().url(url)
        .header("User-Agent", "Mozilla/5.0 (Linux; Android 7.0) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Focus/2.2 Chrome/61.0.3163.98 Mobile Safari/537.36")
        .header("Origin", "*")
        .header("Access-Control-Request-Method", "POST")
        .header("Access-Control-Request-Headers", "Accept, Content-Length")
        .build()
OkHttpClient().newCall(request).execute().use { response ->
    println(response.headers()) // print all headers
    println("------------------------------")
    println("File size: " + response.header("Content-Length"))
    println("Response code: " + response.code())
}

但是在标题列表中找不到Content-Length

X-GUploader-UploadID: AEnB2UriGmFlC7rQhj22k_7ABtzFKZzRsU4WMlIvPXmfIzaHnfrdnk6sENez7kMsE
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: false
Access-Control-Allow-Headers: Accept, Accept-Language, Authorization, Cache-Control, Content-Disposition, Content-Encoding, Content-Language, Content-Length, Content-MD5, Content-Range, Content-Type, Date, GData-Version, google-cloud-resource-prefix, Host, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, Origin, OriginToken, Pragma, Range, Slug, Transfer-Encoding, Want-Digest, x-chrome-connected, X-ClientDetails, X-Client-Version, X-Firebase-Locale, X-GData-Client, X-GData-Key, X-Goog-AdX-Buyer-Impersonation, X-Goog-Api-Client, X-Goog-AuthUser, X-Goog-PageId, X-Goog-Encode-Response-If-Executable, X-Goog-Correlation-Id, X-Goog-Request-Info, X-Goog-Request-Reason, X-Goog-Experiments, x-goog-iam-authority-selector, x-goog-iam-authorization-token, X-Goog-Spatula, X-Goog-Upload-Command, X-Goog-Upload-Content-Disposition, X-Goog-Upload-Content-Length, X-Goog-Upload-Content-Type, X-Goog-Upload-File-Name, X-Goog-Upload-Header-Content-Length, X-Goog-Upload-Offset, X-Goog-Upload-Protocol, X-Goog-Visitor-Id, X-Goog-FieldMask, X-HTTP-Method-Override, X-JavaScript-User-Agent, X-Pan-Versionid, X-Proxied-User-IP, X-Origin, X-Referer, X-Requested-With, X-Upload-Content-Length, X-Upload-Content-Type, X-Use-HTTP-Status-Code-Override, X-Ios-Bundle-Identifier, X-Android-Package, X-Ariane-Xsrf-Token, X-YouTube-VVT, X-YouTube-Page-CL, X-YouTube-Page-Timestamp, X-Goog-Meeting-Botguardid, X-Goog-Meeting-Debugid, X-Goog-Meeting-Token, X-Client-Data, X-Sfdc-Authorization, MIME-Version, Content-Transfer-Encoding
Access-Control-Allow-Methods: GET,OPTIONS
Content-Type: video/mp4
Content-Disposition: attachment;filename="some-name.mp4"
Date: Sun, 31 Mar 2019 14:54:53 GMT
Expires: Sun, 31 Mar 2019 14:54:53 GMT
Cache-Control: private, max-age=0
X-Goog-Hash: crc32c=6SCARA==
Transfer-Encoding: chunked
Server: UploadServer
Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39"
------------------------------
File size: null
Response code: 200

我已经阅读了许多有关CORS的帖子,但是我只发现了服务器到服务器的代码。我正在尝试从客户端(即Android)获取文件信息。那么,您知道要获取吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

请参见RFC 2616 Section 4.4

  

2。如果存在Transfer-Encoding头字段(第14.41节),并且        除“ identity”外具有任何值,则传输长度为        通过使用“块式”传输编码(第3.6节)进行定义,        除非通过关闭连接终止消息。

拥有Content-Length后,您将不会获得Transfer-Encoding: chunked标头。

我认为,如果需要,您必须使用another Google Drive API来获取文件大小。

答案 1 :(得分:1)

我建议使用Google Drive API。您需要在文件API集合中使用 get API 。这是演示link。您可以尝试查看Response标头。这是提取文件API的示例Response标头。您可以在此处查看内容长度。

HTTP/1.1 200 
date: Sun, 31 Mar 2019 17:11:23 GMT
content-encoding: gzip
server: GSE
content-type: application/json; charset=UTF-8
vary: Origin, X-Origin
cache-control: private, max-age=0, must-revalidate, no-transform
content-length: 162
expires: Sun, 31 Mar 2019 17:11:23 GMT

这些步骤可帮助您完成所需的工作:

  1. 在Google API控制台中注册您的应用程序。您可以参考此link
  2. 使用访问令牌,您可以调用download API来流传输内容或下载内容。

请检查此link的示例项目