httpclient读取数据不完整

读取不完整,可能时服务端开启了gzip的原因,至少本人遇到的情况是这样

// 实例化请求对象
            HttpPost request = new HttpPost(Url);
            
            // 设置要发送的数据 以 name=value 键值对
            HttpEntity requestEntity = new UrlEncodedFormEntity(params, "UTF-8");
            request.setEntity(requestEntity);
            //获取HttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            
            httpClient.addRequestInterceptor(new HttpRequestInterceptor() {  
                public void process(final HttpRequest request,  
                        final HttpContext context) throws HttpException,  
                        IOException {  
                    if (!request.containsHeader("Accept-Encoding")) {  
                        request.addHeader("Accept-Encoding", "gzip");  
                    }  
                }  
              
            });  

            //设置请求超时时间
            /tpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
            // 发送请求 其实底层就是 OutputStream,并获得响应
            HttpResponse response = httpClient.execute(request);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 获得服务器响应返回的数据
                //         
                HttpEntity entity = response.getEntity();
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    for (HeaderElement element : ceheader.getElements()) {
                        if ("gzip".equalsIgnoreCase(element.getName())) {
                            entity = new GzipDecompressingEntity(response.getEntity());
                            break;
                        }
                    }
                }

                String html = EntityUtils.toString(entity, "UTF-8");
                System.out.println(html);

你可能感兴趣的:(httpclient读取数据不完整)