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
基础教程推荐
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 Windows 证书导入 Java 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- JPA惰性列表上的流 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
