http请求其他接口的utils
首先在pom中加入
关于http需要的jar包 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.4.1</version> </dependency> json解析的jar包 <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
因为http请求完成之后返回的数据是json数据,所以我们需要用json的jar包来解析数据,下面是java代码
/**. * @Description: TODO(调用接口获取数据) * @author xiehj * @date 2020年8月19日 下午3:20:05 * @version V1.0 */ public class MpcStaffInfoUtil { //这里需要导入log的jar包 private static final Logger log = Logger.getLogger(MpcStaffInfoUtil.class); /** * 发送post请求的方法 */ public static String pushPost(String code) { //这里是判断code参数是否为空的判断 if(StringUtils.isNotBlank(code)) { log.info("调用根据code查询数据接口的方法。"); } else { log.info("调用查询全量数据接口的方法。"); } //CloseableHttpClient httpClient = HttpClients.createDefault(); // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //HttpClient httpClient = new DefaultHttpClient(); //设置连接超时 connetionTimeout:指客户端和服务器建立连接的timeout, //setSocketTimeout 设置连接超时 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000) .setSocketTimeout(60000).setConnectTimeout(120000).build(); //请求的url String url = null; //这里是根据code来判断到底该用配置文件里面的那个接口 if (StringUtils.isNotBlank(code)) { //readParperties.getSftp这个方法地址 https://www.cnblogs.com/xhj-java/p/13536697.html url = readParperties.getSfpt("/mpcstaffinfo.properties", "TEST_URL_CODE"); url = url + code; } else { url = readParperties.getSfpt("/mpcstaffinfo.properties", "TEST_URL"); } log.info("请求的url:" + url); //String url = "http://127.0.0.1:8080/hrbase-web/httpRestInterface/listStaffInfo?StaffCode="+code; CloseableHttpResponse response = null; //HttpResponse response = null; try { //替换空格 url= url.replaceAll(" ", "%20"); //连接远程服务 HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); /* InputStreamEntity ise = new InputStreamEntity(is, -1L); httpPost.setEntity(ise);//设置传入流 */
response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); log.info("HTTP响应状态为:" + response.getStatusLine()); if (responseEntity != null) { log.info("HTTP响应内容长度为:" + responseEntity.getContentLength()); // 主动设置编码,来防止响应乱码 String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8); //log.info("HTTP响应内容为:" + responseStr); return responseStr; } } catch (Exception e) { e.printStackTrace(); }/*finally{ try { // 释放资源 if (httpClient != null) { httpClient.close(); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } }*/ return null; } //StaffInfoEntity 是数据里面的实体类 public static List<StaffInfoEntity> getMpc(String code) { String pushPost = MpcStaffInfoUtil.pushPost(code); List<StaffInfoEntity> staToMpc = null; JSONObject jsStr = JSONObject.fromObject(pushPost); //result 是返回的数据的名称 即 map.put("result",list); String string = jsStr.getString("result"); /*try { String string = jsStr.getString("result"); } catch (JSONException e) { System.out.println("取不到result的值!"); return staToMpc; }*/ if(StringUtils.isNotBlank(string)) { final Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); final List<StaffInfoEntity> res = gson.fromJson(string, new TypeToken<List<StaffInfoEntity>>() { }.getType()); return staToMpc; } return staToMpc; }
一定要爱着点儿什么,恰似草木对光阴的钟情。