如何在okhttp中将Charset自动添加到Content-Type中

Oli*_*ler 14 java google-cloud-storage okhttp pre-signed-url

请考虑以下代码:

    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();
Run Code Online (Sandbox Code Playgroud)

我从客户端访问GCS,并使用以前签名的URL.

问题:似乎okhttp将为身体[A]声明的字符集添加到URL中(至少对于text/plain),即使它未在[B]中声明.这会弄乱我签名的URL,GCS会返回403 Forbidden.

  • 如果我从[A]中删除字符集,它仍会被添加.
  • 如果我在签名之前将charset添加到签名URL,它就可以正常运行,GCS会返回200 OK.

但这不是应该的.至少在使用签名URL时,必须将这些URL完全按照声明的方式发送到服务器.

我尝试使用Apache http客户端(我不想在生产中使用,因为okhttpclient已经是我的安装的一部分)并且该客户端不会暴露这种行为:

        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();
Run Code Online (Sandbox Code Playgroud)

有没有办法抑制okhttp中的行为,它是添加到Content-Type还是冗余地传输了Body-Type中的Content-Type?

Oli*_*ler 23

我找到了解决方案:

以下行是罪魁祸首:

RequestBody body = RequestBody.create(mediaType, media);
Run Code Online (Sandbox Code Playgroud)

创建有3个媒体签名:

  • 串
  • 字节[]
  • 文件

当我传递一个String时,它忽略了提供的mediaType并将charset添加到它.即使是图像/ jpeg它也会发送

图像/ JPEG; 字符集= utf-8的

到服务器.

使用byte []或File可以抑制该行为.

我希望这可以帮助你!

[愚蠢我 - 为了简单起见,我在测试过程中给它一个字符串,因为我不关心身体;-(]