public static String doPost(String url){
//创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = "";
try{
//创建http请求
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
//创建请求内容
String jsonStr = "{\"qry_by\":\"name\", \"name\":\"Tim\"}";
StringEntity entity = new StringEntity(jsonStr);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭资源
if(response != null){
try {
response.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
if(httpClient != null){
try{
httpClient.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
}
return result;
}
public static String doGet(String url){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = "";
try{
//通过默认配置创建一个httpClient实例
httpClient = HttpClients.createDefault();
//创建httpGet远程连接实例
HttpGet httpGet = new HttpGet(url);
//httpGet.addHeader("Connection", "keep-alive");
//设置请求头信息
httpGet.addHeader("Accept", "application/json");
//配置请求参数
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(35000) //设置连接主机服务超时时间
.setConnectionRequestTimeout(35000)//设置请求超时时间
.setSocketTimeout(60000)//设置数据读取超时时间
.build();
//为httpGet实例设置配置
httpGet.setConfig(requestConfig);
//执行get请求得到返回对象
response = httpClient.execute(httpGet);
//通过返回对象获取返回数据
HttpEntity entity = response.getEntity();
//通过EntityUtils中的toString方法将结果转换为字符串,后续根据需要处理对应的reponse code
result = EntityUtils.toString(entity);
System.out.println(result);
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException ioe){
ioe.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭资源
if(response != null){
try {
response.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
if(httpClient != null){
try{
httpClient.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
}
return result;
}