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;
}
http请求其他接口的utils的更多相关文章
- webapi+Task并行请求不同接口实例
标题的名称定义不知道是否准确,不过我想表达的意思就是使用Task特性来同时请求多个不同的接口,然后合并数据:我想这种场景的开发对于对接过其他公司接口的人不会陌生,本人也是列属于之内,更多的是使用最原始 ...
- 代码片段:处理HTTP请求的接口
以下程序用来获取用户传递过来的信息. 1.头文件 /* * 这个是处理HTTP请求的接口头文件 */ #define KEY_VALUE_MAX 1024 /* 键值的最大长度 */ typedef ...
- 通过HttpWebRequest请求https接口
一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...
- node请求下载接口时乱码
先说下问题 之前做的一个项目,三端同时开发(PC.WEB.APP),由于架构方面的原因,服务均不对外开放,接口地址自然也就不对外暴露了,所有请求都要经过node转发,此为背景.... 网站有个扫描二维 ...
- python接口自动化(八)--发送post请求的接口(详解)
简介 上篇介绍完发送get请求的接口,大家必然联想到发送post请求的接口也不会太难,被聪明的你又猜到了.答案是对的,虽然发送post请求的参考例子很简单,但是实际遇到的情况却是很复杂的,因为所有系统 ...
- java编程(2)——servlet和Ajax异步请求的接口编程(有调用数据库的数据)
第一步: 1.为项目配置 Tomcat 为 server: 2.导入 mysql的jar包 到项目目录中: 第二步:编码 1.数据库连接类ConnectMysql.java代码: package co ...
- java编程(1)——servlet和Ajax异步请求的接口编程(没有调用数据库的数据)
编程应用背景: 使用HttpServlet接口来编写一个动态登录的接口(需要在Tomcat容器发布) 登录的 LoginSample 类代码: package com.zhang.java; publ ...
- c#POST请求php接口
POST请求php接口 /// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> /// <param name= ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...
- ajax请求jesery接口无法获取参数的问题解决方案
jesery是强大的RESTful api框架, 很多人在用它做web项目时会遇到这样一个问题: ajax请求jesery接口无法获取输入参数, 可明明接口已经指明了Consume是applicati ...
随机推荐
- 网络框架重构之路plain2.0(c++23 without module) 综述
最近互联网行业一片哀叹,这是受到三年影响的后遗症,许多的公司也未能挺过寒冬,一些外资也开始撤出市场,因此许多的IT从业人员加入失业的行列,而且由于公司较少导致许多人求职进度缓慢,很不幸本人也是其中之一 ...
- 太坑了吧!一次某某云上的redis读超时排查经历
一次排查某某云上的redis读超时经历 性能排查,服务监控方面的知识往往涉及量广且比较零散,如何较为系统化的分析和解决问题,建立其对性能排查,性能优化的思路,我将在这个系列里给出我的答案. 问题背景 ...
- CVE-2022-21454:漏洞整改mysql5.7.37升级至5.7.38 tar包升级
问题描述:对数据库服务器进行漏扫,发现一些中高位漏洞需要整改,有些数据库需要升级到最新版 漏洞修改指导链接:https://www.oracle.com/security-alerts/cpuapr2 ...
- 帝国cms将没有搜索到结果的关键字存入到数据库的方法
在帝国cms网站前台搜索一个关键字,如果在网站中查询到了,这个关键字会被记录入搜索关键字表中,但是如果在网站中没有搜索到,就不会记录入搜索关键字表中,那怎么把没有搜索结果的关键字才能记录到数据库中,方 ...
- 10分钟理解React生命周期
前言 学习React,生命周期很重要,我们了解完生命周期的各个组件,对写高性能组件会有很大的帮助. 一.简介 React /riˈækt/ 组件的生命周期指的是组件从创建到销毁过程中所经历的一系列方法 ...
- 【原型设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
简介 原型模式(Prototype Pattern)是一种创建型设计模式,使你能够复制已有对象,而无需使代码依赖它们所属的类,同时又能保证性能. 这种模式是实现了一个原型接口,该接口用于创建当前对象的 ...
- 当我第一次通过Kotlin和Compose来实现一个Canvas时, 我收获了什么?
当我第一次通过Kotlin和Compose来实现一个Canvas时, 我收获了什么? 自从2019年Google推荐Kotlin为Android开发的首选语言以来已经经历了将近四年的时间, Compo ...
- 数据结构(DataStructure)-01
数据结构-01 **数据结构与算法** **算法概述** **时间复杂度概述** **时间复杂度 - 计算规则** **数据结构概述** **抽象数据类型** **线性表 - 顺序表** **线性表 ...
- ✗ CocoaPods not installed.
mac 配置 flutter 会提示许多 关于xcode的 如图 显示 ✗ CocoaPods installed but not initialized. 其实最开始提示的是 ✗ CocoaPods ...
- ARC143
ARC143 考试情况:一眼订正,鉴定为做出前三题. A - Three Integers 以前做过 \(n\) 个数的版本,当时还被某人嘲讽说"堆,贪心,这都做不出来?". \( ...