小编Pet*_*ron的帖子

Apache HttpClient响应内容长度返回-1

为什么以下代码返回-1?似乎请求失败.

public static void main(String[] args)
{
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://www.google.de");

    HttpResponse response;
    try
    {
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);

        // Prints -1
        System.out.println(entity.getContentLength());
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        httpGet.releaseConnection();
    }
}
Run Code Online (Sandbox Code Playgroud)

是否可以将响应作为String?

java http apache-httpcomponents

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

apache-httpcomponents ×1

http ×1

java ×1