HttpClientUtil工具类

HttpClientUtil.java

1package com.bjsxt.springboothelloworld; 2 3import java.io.IOException; 4import java.net.URI; 5import java.util.ArrayList; 6import java.util.List; 7import java.util.Map; 8 9import org.apache.http.NameValuePair; 10import org.apache.http.client.entity.UrlEncodedFormEntity; 11import org.apache.http.client.methods.CloseableHttpResponse; 12import org.apache.http.client.methods.HttpGet; 13import org.apache.http.client.methods.HttpPost; 14import org.apache.http.client.utils.URIBuilder; 15import org.apache.http.entity.ContentType; 16import org.apache.http.entity.StringEntity; 17import org.apache.http.impl.client.CloseableHttpClient; 18import org.apache.http.impl.client.HttpClients; 19import org.apache.http.message.BasicNameValuePair; 20import org.apache.http.util.EntityUtils; 21 22public class HttpClientUtil { 23 24 public static String doGet(String url, Map<String, String> param) { 25 26 // 创建Httpclient对象 27 CloseableHttpClient httpclient = HttpClients.createDefault(); 28 29 String resultString = ""; 30 CloseableHttpResponse response = null; 31 try { 32 // 创建uri 33 URIBuilder builder = new URIBuilder(url); 34 if (param != null) { 35 for (String key : param.keySet()) { 36 builder.addParameter(key, param.get(key)); 37 } 38 } 39 URI uri = builder.build(); 40 41 // 创建http GET请求 42 HttpGet httpGet = new HttpGet(uri); 43 44 // 执行请求 45 response = httpclient.execute(httpGet); 46 // 判断返回状态是否为200 47 if (response.getStatusLine().getStatusCode() == 200) { 48 resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 49 } 50 } catch (Exception e) { 51 e.printStackTrace(); 52 } finally { 53 try { 54 if (response != null) { 55 response.close(); 56 } 57 httpclient.close(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 } 62 return resultString; 63 } 64 65 public static String doGet(String url) { 66 return doGet(url, null); 67 } 68 69 public static String doPost(String url, Map<String, String> param) { 70 // 创建Httpclient对象 71 CloseableHttpClient httpClient = HttpClients.createDefault(); 72 CloseableHttpResponse response = null; 73 String resultString = ""; 74 try { 75 // 创建Http Post请求 76 HttpPost httpPost = new HttpPost(url); 77 // 创建参数列表 78 if (param != null) { 79 List<NameValuePair> paramList = new ArrayList<>(); 80 for (String key : param.keySet()) { 81 paramList.add(new BasicNameValuePair(key, param.get(key))); 82 } 83 // 模拟表单 84 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); 85 httpPost.setEntity(entity); 86 } 87 // 执行http请求 88 response = httpClient.execute(httpPost); 89 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } finally { 93 try { 94 response.close(); 95 } catch (IOException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 } 100 101 return resultString; 102 } 103 104 public static String doPost(String url) { 105 return doPost(url, null); 106 } 107 108 public static String doPostJson(String url, String json) { 109 // 创建Httpclient对象 110 CloseableHttpClient httpClient = HttpClients.createDefault(); 111 CloseableHttpResponse response = null; 112 String resultString = ""; 113 try { 114 // 创建Http Post请求 115 HttpPost httpPost = new HttpPost(url); 116 // 创建请求内容 117 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); 118 httpPost.setEntity(entity); 119 // 执行http请求 120 response = httpClient.execute(httpPost); 121 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } finally { 125 try { 126 response.close(); 127 } catch (IOException e) { 128 // TODO Auto-generated catch block 129 e.printStackTrace(); 130 } 131 } 132 133 return resultString; 134 } 135 136 public static void main(String[] args) { 137 String url ="http://127.0.0.1:9090/shutdown"; 138 //该url必须要使用dopost方式来发送 139 HttpClientUtil.doPost(url); 140 } 141}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )