HttpClient 4.5.3无法获得对字​​符串

时间:2017-07-05 13:24:46

标签: java httpclient

当我使用Apache的HttpClient 4.5.3调用REST服务时,我收到此错误。

public class httpsTeste {

    /**
     * @param args
     * @throws IOException
     * @throws ClientProtocolException
     */
    static String USER = "user";
    static String PWD = "password";

    public static void main(String[] args) throws ClientProtocolException,
    IOException {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(null, 9098),
                new UsernamePasswordCredentials(USER, PWD));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();

        try {
            HttpGet httpget2 = new HttpGet(
                    "http://localhost:9098/rest/bpm/wle/v1/process/566512?parts=header");
            System.out
            .println("Executing request " + httpget2.getRequestLine());

            CloseableHttpResponse response2 = httpclient.execute(httpget2);     
            StringBuilder sb = new StringBuilder();
            sb.append(EntityUtils.toString(response2.getEntity()));

            System.out.println(sb.toString());


            try {
                System.out.println("----------------------------------------");
                System.out.println(response2.getStatusLine());
            }finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

输出:

Executing request GET http://localhost:9098/rest/bpm/wle/v1/process/566512?parts=header HTTP/1.1
Exception in thread "main" org.apache.http.TruncatedChunkException: Truncated chunk ( expected size: 24469; actual size: 11082)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:198)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
    at java.util.zip.InflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at java.util.zip.GZIPInputStream.read(Unknown Source)
    at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:70)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.Reader.read(Unknown Source)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:225)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:306)
    at com.br.teste.httpsTeste.main(httpsTeste.java:60)

这会发生一些请求。有时当响应较小时,它通常返回实体并将其转换为字符串。

当我调用完全相同的代码但没有String转换和System.out.println时,不会发生此错误。

这到底出了什么问题?我也尝试过:

String s = EntityUtils.toString(response2.getEntity());

但问题仍然存在。

0 个答案:

没有答案