//get请求
//创建httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//请求路径
String Url="";
//设置请求头相关信息
HttpGet httpGet = new HttpGet(Url);
httpGet.addHeader("a", "a");
httpGet.addHeader("b", "b");
httpGet.addHeader("c", "c");
httpGet.addHeader("d", "d");
//设置请求配置相关信息
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
.setConnectionRequestTimeout(3000).setSocketTimeout(3000).build();
//携带请求配置
httpGet.setConfig(requestConfig);
// 执行请求
CloseableHttpResponse response = httpclient.execute(httpGet);
// 取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
//解析响应结果
String resp = EntityUtils.toString(response.getEntity(), "utf-8");
//post请求
//创建httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 模拟 POST/PUT 的 body 中数据,需转为 JSON 进行签名。GET 则没有这部分内容。
CloseableHttpClient httpclient1 = HttpClients.createDefault();
String url="";
HttpPost httpPost=new HttpPost(url);
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("a", "a");
String bodyParam = new Gson().toJson(dataMap);
StringEntity bodyData = new StringEntity(bodyParam.toString(), "UTF-8");
httpPost.setEntity(bodyData);
httpPost.addHeader("a", "a");
String urlStr = httpPost.getURI().toString();
RequestConfig requestConfig1 = RequestConfig.custom().setConnectTimeout(3000)
.setConnectionRequestTimeout(3000).setSocketTimeout(3000).build();
httpPost.setConfig(requestConfig1);
// 执行请求
CloseableHttpResponse response = httpclient.execute(httpPost);
// 取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
String resp = EntityUtils.toString(response.getEntity(), "utf-8");
}