我在 http 和 https 站点(相同的 IIS 服务器)上公开了 JSON 服务。使用此代码,当调用 http url 时,我得到了 gziped 响应,但调用 https 响应不是 gziped。HTTPS 正在使用同一站点的另一个应用程序上重新调整压缩后的内容和正确的标头(在这种情况下,想要的标头是 Content-Encoding)。
private InputStream execute(String url, int method, JSONObject jsonObject) throws ClientProtocolException, IOException {
DefaultHttpClient httpsclient = new DefaultHttpClient();
InputStream inStream = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept-Encoding", "gzip");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json; charset=utf-8");
// adding post params
if (jsonObject != null) {
StringEntity entity = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
}
HttpResponse httpRes = httpsclient.execute(httpPost);
inStream = httpRes.getEntity().getContent();
Header contentEncoding = httpRes.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
return new GZIPInputStream(inStream);
} else {
return inStream;
}
} else if (method == GET) {
throw new UnsupportedOperationException("GET not implemented yet.");
} else {
throw new UnsupportedOperationException();
}
}
有谁知道这里有什么问题,或者我应该使用其他方法吗?contentEncoding 标头为空