我有这个代码,它运行正常:
System.out.println("Fetching...");
CloseableHttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet("http://blogs.reuters.com/talesfromthetrail/2012/06/26/obama-letter-brings-democratic-donors-out-of-the-woodwork/");
CloseableHttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Fetched!");
return EntityUtils.toString(entity);
}
但是建议不要使用EntityUtils
将实体转换为String。所以我尝试使用StringBuffer
,例如示例代码:
System.out.println("Fetching...");
CloseableHttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet("http://blogs.reuters.com/talesfromthetrail/2012/06/26/obama-letter-brings-democratic-donors-out-of-the-woodwork/");
CloseableHttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Fetched!");
BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer result = new StringBuffer();
String line = rd.readLine();
while (line != null) {
result.append(line);
line = rd.readLine();
}
return result.toString();
}
它挂了! "Fetched!"
打印语句甚至无法执行。我是一个Java菜鸟,所以我不知道为什么会这样。我该怎么调试呢?