请考虑以下代码:
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();
我从客户端访问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();
有没有办法抑制okhttp中的行为,它是添加到Content-Type还是冗余地传输了Body-Type中的Content-Type?