How to suppress Charset being automatically added to Content-Type in okhttp(如何禁止字符集自动添加到 okhttp 中的 Content-Type)
问题描述
考虑以下代码:
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); // [A]
RequestBody body = RequestBody.create(mediaType, media);
String[] aclHeader = "x-goog-acl:public-read".split(":");
Request request = new Request.Builder()
.addHeader("Content-Type", "text/plain") // [B]
.addHeader(aclHeader[0], aclHeader[1])
.url(url)
.put(body)
.build();
Response response = client.newCall(request).execute();
我正在使用以前签名的 URL 从客户端访问 GCS.
I am accessing GCS from a client, with a previously signed URL.
问题:似乎 okhttp 还将为正文 [A] 声明的字符集添加到 URL(至少对于 text/plain),即使它没有在 [B] 中声明.这弄乱了我的签名 URL,GCS 返回 403 Forbidden.
Problem: It seems okhttp adds the charset declared for the body [A] to the URL as well (at least for text/plain), even though it is not declared in [B]. This messes up my signed URL and GCS returns 403 Forbidden.
- 如果我从 [A] 中删除字符集,它仍然会被添加.
- 如果我在签名之前将字符集添加到签名的 URL,它会起作用并且 GCS 返回 200 OK.
但这不是应该的.至少在使用签名 URL 时,这些 URL 必须完全按照声明发送到服务器.
But this is not as it should be. At least when working with signed URLs, these must be sent to the server exactly as declared.
我尝试使用 Apache http 客户端(我不想在生产中使用它,因为 okhttpclient 已经是我安装的一部分)并且该客户端没有公开此行为:
I tried using the Apache http client (which I don't want to use in production as okhttpclient is already part of my installation) and that client does not expose this behavior:
String[] aclHeader = "x-goog-acl:public-read".split(":");
StatusLine statusLine = Request
.Put(url)
.addHeader("Content-Type", "text/plain")
.addHeader(aclHeader[0], aclHeader[1])
.bodyByteArray(media)
.execute().returnResponse().getStatusLine();
有没有办法抑制 okhttp 中的行为,它添加到 Content-Type 或在正文中冗余传输 Content-Type?
Is there a way to suppress the behavior in okhttp, that it adds to the Content-Type or transfers the Content-Type within the body redundantly?
推荐答案
我找到了解决方案:
下面这行是罪魁祸首:
RequestBody body = RequestBody.create(mediaType, media);
create 有 3 个媒体签名:
create has 3 signatures for media:
- 字符串
- 字节[]
- 文件
当我传递一个字符串时,它会忽略提供的 mediaType 并将字符集添加到它.即使对于图像/JPEG,它也会发送
When I pass a String, it disregards the supplied mediaType and adds the charset to it. Even for image/jpeg it would send
图像/JPEG;字符集=utf-8
image/jpeg; charset=utf-8
到服务器.
使用 byte[] 或 File 会抑制这种行为.
Using byte[] or File suppresses that behavior.
希望对你有帮助!
[愚蠢的我——为了简单起见,我在测试期间给了它一个字符串,因为我不关心身体;-( ]
[Stupid me - for simplicity I gave it a String during testing, as I didn't care about the body ;-( ]
这篇关于如何禁止字符集自动添加到 okhttp 中的 Content-Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何禁止字符集自动添加到 okhttp 中的 Content-Type
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01