此问题是问题here的扩展。我使用下面复制的代码here来压缩一个JSONObject
。
String foo = "value";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(foo.getBytes("UTF-8"));
} finally {
if (gzos != null) try { gzos.close(); } catch (IOException ignore) {};
}
byte[] fooGzippedBytes = baos.toByteArray();
我使用DefaultHttpClient
将这个压缩的JSONObject发送到服务器(代码在我的控制之下)。
我的问题
我应该在我的request
中使用什么标题?我正在使用request.setHeader("Content-type", "application/json");
将JSON发送到服务器?
发布于 2012-07-11 07:14:52
要通知服务器您正在发送gzip编码的数据,请发送Content-Encoding标头,而不是Accept-Encoding。
发布于 2012-07-11 06:38:21
This answer显示您需要设置一个标头,表明您正在发送压缩数据:
HttpUriRequest request = new HttpGet(url);
request.addHeader("Content-Encoding", "gzip");
// ...
httpClient.execute(request);
答案还显示了如何处理传入的压缩数据。
发布于 2020-01-31 15:40:29
Content-Type:application/json
-告知http callContent-Encoding: gzip
中的内容数据类型-告知所使用的压缩。application/json
或text/xml
或其他类型可以压缩为gzip并发送给接收方,接收方将识别传入的数据是json/xml/text
类型,然后转换回json/xml/text类型的对象。
有了gzip头,只有接收方才能识别传入的数据正在进行Content-Encoding
压缩。然后接收方需要解压缩传入的数据并使用它。
https://stackoverflow.com/questions/11414987
复制相似问题