无法使用DefaultHttpClient,HttpPost JSON在Android设备上获取gziped InputSTream

时间:2015-02-25 08:27:52

标签: java android json web-services content-encoding

我在http和https站点(相同的IIS服务器)上公开了JSON服务。使用此代码,当调用http url时,我得到gziped响应,但调用https响应不是gziped。 HTTPS正在使用其他正在使用相同网站的应用程序重新生成gziped内容和正确的标头(在这种情况下需要标头是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标头为空

0 个答案:

没有答案