HttpURLConnection PUT to Google Cloud Storage giving error 403(HttpURLConnection PUT 到谷歌云存储给出错误 403)
问题描述
我尝试使用 XML API 将文件上传到 Google Cloud Storage.我为每次上传生成了正确的 GoogleAccessId、到期日期和签名.奇怪的是,我可以使用 Postman(Chrome 应用程序)PUT 文件,所以我确定 URL 没问题.我只是无法使用我的 Android Java 程序放置它(它返回给我 403 错误).执行上传的源代码在这里(它基于这个:https://cloud.google.com/storage/docs/access-control#Signing-Strings):
I tried to upload a file to Google Cloud Storage using XML API. I have the right GoogleAccessId, expiry date and signature generated for each upload. The strange thing is that I can PUT file using Postman (application for Chrome), so I'm sure that the URL is ok. I just cannot PUT it using my Android Java program (it returns to me 403 error). The source code performing upload is here (it base on this one: https://cloud.google.com/storage/docs/access-control#Signing-Strings):
URL url;
HttpURLConnection connection;
try {
url = new URL("http://google-testbucket.storage.googleapis.com/testdata.txt?GoogleAccessId=1234567890123@developer.gserviceaccount.com&Expires=1331155464&Signature=BClz9e4UA2MRRDX62TPd8sNpUCxVsqUDG3YGPWvPcwN%2BmWBPqwgUYcOSszCPlgWREeF7oPGowkeKk7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9sFpqXsQI8IQi1493mw%3D");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("Test");
out.close();
Log.i("TAG", "PUT Response code: " + connection.getResponseCode());
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e("TAG", "MalformedURLException");
} catch (ProtocolException e) {
e.printStackTrace();
Log.e("TAG", "ProtocolException");
} catch (IOException e) {
e.printStackTrace();
Log.e("TAG", "IOException");
}
PUT 对象的文档:https://cloud.google.com/storage/docs/xml-api/put-object-upload
任何人都可以调查这个问题并给我提示这个问题可能出了什么问题吗?
Can anybody look into this problem and give me hints what might went wrong with this one?
推荐答案
我刚刚发现 HttpURLConnection
添加了 Content-Type
标头,其值为 application/x-www-form-urlencoded
本身.我已经在我的 android 模拟器上使用 HTTP 嗅探器完成了它.
I just figured out that HttpURLConnection
adds Content-Type
header with value application/x-www-form-urlencoded
by itself. I've done it using HTTP sniffer on my android emulator.
此自动添加的标头导致签名不匹配.在我更改服务器端的代码以允许使用 Content-Type: application/x-www-form-urlencoded
的请求后,它会生成正确的签名并且工作正常.
This auto-added header caused signature mismatch. After I changed the code on the server-side to allow requests with Content-Type: application/x-www-form-urlencoded
it generates the right signature and it works fine.
感谢@morpheus05 的承诺.
Thank you @morpheus05 for your commitment.
这篇关于HttpURLConnection PUT 到谷歌云存储给出错误 403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HttpURLConnection PUT 到谷歌云存储给出错误 403
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01