//DefaultHttpClient方式发送get请求
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
}catch(Exception e){
System.out.println("exception:"+e);
}
finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
//HttpClient方式发送get请求, DefaultHttpClient方式已过时
public static String doGet(String url){
String body = "{}";
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
//获取响应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity);
}
} finally {
response.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
}catch(Exception e){
System.out.println("exception:"+e);
}
finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
//HttpClient方式发送get请求, DefaultHttpClient方式已过时
public static String doGet(String url){
String body = "{}";
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
//获取响应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity);
}
} finally {
response.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}