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 ...
随机推荐
- docker安装python+nginx
一个容器安装python和nginx dockerfile FROM centos:7.9.2009 USER root RUN yum install gcc openssl-devel bzip2 ...
- Java GC基础知识
对象存活判断 引用计数 在对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加一:当引用失效时,计数器值就减一:任何时刻计数器为零的对象就是不可 能再被使用的 引用计数法的缺陷: publi ...
- 【Java SE】反射
Java Reflection 1 Java 反射机制概述 Reflection反射被视为动态语言的关键,反射机制允许在运行期间借助于Reflection取得任何类的内部信息,并能直接操作任意对象的内 ...
- Java Stream常见用法汇总,开发效率大幅提升
本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...
- CSS3-页面布局基础一
一.CSS概要 web前端开发者最最注的内容是三个:HTML.CSS与JavaScript,他们分别在不同方面发挥自己的作用,HTML实现页面结构,CSS完成页面的表现与风格,JavaScript实现 ...
- 吃透SpringMVC面试八股文
说说你对 SpringMVC 的理解 SpringMVC是一种基于 Java 的实现MVC设计模型的请求驱动类型的轻量级Web框架,属于Spring框架的一个模块. 它通过一套注解,让一个简单的Jav ...
- include-file
0X01 前言 这篇文章介绍文件包含漏洞. 0X02 最常见的两个函数的形象解释: 我们知道文件包含最常见的是两个函数 include() require()(这里就不谈他们的亲戚 include_o ...
- 前端 本地缓存localStorage/sessionStorage
当我们刷新页面时,除了路由,页面的当前状态及数据会全部清空/重置,包括浏览器标题. 如果想保存刷新前的一些数据,可以通过window.localStorage/sessionStorage,在浏览器里 ...
- 第一章:PyTorch 入门
第一章:PyTorch 入门 1.1 Pytorch 简介 1.1.1 PyTorch的由来 1.1.2 Torch是什么? 1.1.3 重新介绍 PyTorch 1.1.4 对比PyTorch和Te ...
- [OpenCV-Python] 7 把鼠标当画笔
文章目录 OpenCV-Python: II OpenCV 中的 Gui 特性 7 把鼠标当画笔 7.1 简单演示 7.2 高级一点的示例 OpenCV-Python: II OpenCV 中的 Gu ...