2、HttpClientUtil工具类org.apache.httpcomponents httpclient4.5.2
3、Get请求package com.task.test.utils; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException; @Slf4j public class HttpClientUtil { public static String doGet(String url) { CloseableHttpClient closeableHttpClient = null; HttpGet httpGet = null; String respone = null; try{ closeableHttpClient = HttpClientBuilder.create().build(); RequestConfig build = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build(); httpGet = new HttpGet(url); httpGet.setConfig(build); CloseableHttpResponse execute = closeableHttpClient.execute(httpGet); if(execute!=null){ HttpEntity entity = execute.getEntity(); if(entity!=null){ respone = EntityUtils.toString(entity,"UTF-8"); } } execute.close(); }catch (Exception e){ log.info("请求接口url:{},异常信息:{}",url,e.getMessage()); e.printStackTrace(); }finally { try{ closeableHttpClient.close(); }catch (Exception e){ e.getMessage(); } } return respone; } public static String doPost(String url,String context){ CloseableHttpClient httpClient = null; HttpPost httpPost = null; String result = null; try{ httpClient = HttpClientBuilder.create().build(); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000).setSocketTimeout(10000).setConnectTimeout(10000).build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); StringEntity stringEntity = new StringEntity(context,"utf-8"); stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity); CloseableHttpResponse response = httpClient.execute(httpPost); if(response != null){ HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,"UTF-8"); } } response.close(); System.out.print(httpClient); }catch(Exception ex){ log.info("请求接口url:{},异常信息:{}",url,ex.getMessage()); ex.printStackTrace(); } finally{ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } }
4、Post请求@GetMapping("/list") public String list() throws IOException { String url = "http://localhost:12006/templatePublic/list"; String result = HttpClientUtil.doGet(url); return result; }
创新互联公司秉承实现全网价值营销的理念,以专业定制企业官网,做网站、成都网站制作,小程序设计,网页设计制作,手机网站制作,全网营销推广帮助传统企业实现“互联网+”转型升级专业定制企业官网,公司注重人才、技术和管理,汇聚了一批优秀的互联网技术人才,对客户都以感恩的心态奉献自己的专业和所长。@PostMapping("/add") public String add(@RequestBody TbTemplatePublic tbTemplatePublic){ JSONObject jsonObject = new JSONObject(); jsonObject.put("templateType", tbTemplatePublic.getTemplateType()); jsonObject.put("policeCode", tbTemplatePublic.getPoliceCode()); jsonObject.put("templateContent", tbTemplatePublic.getTemplateContent()); jsonObject.put("templateTitle", tbTemplatePublic.getTemplateTitle()); jsonObject.put("fraudType", tbTemplatePublic.getFraudType()); String result = HttpClientUtil.doPost("http://localhost:12006/templatePublic/add", jsonObject.toJSONString()); return result; }
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧