接口调用工具类

public class HttpClientApi {

public static JSONObject getData(String url,String data) {

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
httpPost.setEntity(new StringEntity(data));
HttpResponse response = httpClient.execute(httpPost);
JSONObject jsonObject = JSONObject.parseObject(EntityUtils
.toString(response.getEntity()));
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

//传String 接收JSONArray
public static JSONArray getData2(String url,String data) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
httpPost.setEntity(new StringEntity(data));
HttpResponse response = httpClient.execute(httpPost);
JSONArray JSONArrays = JSONArray.parseArray(EntityUtils
.toString(response.getEntity()));
return JSONArrays;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String getData3(String url, JSONObject jsonObject) {
//url = Config.get("ac-product-api.url") + url;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(180 * 1000)
.setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(jsonObject.toString(),
ContentType.create("application/json", "utf-8")));
HttpResponse response = httpClient.execute(httpPost);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
return "post failure :caused by-->" + e.getMessage().toString();
}
}

//传json 接收JSONArray
public static JSONArray getData4(String url, JSONObject jsonObject) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(180 * 1000)
.setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(jsonObject.toString(),
"UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
JSONArray JSONArrays = JSONArray.parseArray(EntityUtils
.toString(response.getEntity(),"UTF-8"));
return JSONArrays;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}

原文地址:https://www.cnblogs.com/lifan12589/p/11712092.html

时间: 2024-08-30 13:35:44

接口调用工具类的相关文章

Spring 远程调用工具类RestTemplateUtils

RestTemplateUtils.java package utils; import java.util.Map; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import or

(转)Spring 远程调用工具类RestTemplateUtils

出处:https://www.cnblogs.com/jonban/p/rest.html Spring 远程调用Rest服务工具类,包含Get.Post.Put.Delete四种调用方式. 依赖jar <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.9.RELEASE<

zookeeper-kafka(集群版)安装部署以及java调用工具类

Kafka安装部署文档 ■ 文档版本 V1.0 ■ 操作系统 CentOS Linux release 7.3.1611 ■ 编写人员 闫立雄 ■ 文档日期 2019-01-06 一.  概述 该文档详细描述了在Linux环境下安装Kafka和ZooKeeper的全过程,文档中以kafka_2.11-1.1.0.tgz和zookeeper-3.4.12.tar.gz为例. 二.  安装ZooKeeper 2.1  下载ZooKeeper 方式1   http://mirrors.shu.edu.

CXF soup webservice 动态客户端调用工具类

在尝试了多种webservice客户端调用方法之后,还是觉得这种方法靠谱点儿,此方法用到了Apache的CXF框架,工具类源码如下: import java.lang.reflect.Method; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class WsReq{     private String wsUrl;     private Class<?> interfaceClz;          pulbic W

接口调用实现类&amp;&amp; 为什么Autowired定义在接口上

1.接口与回调 package edu.cqu.interfaceTest; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import javax.swing.JOptionPane; import javax.swing.Timer; public class TimeTest { public s

接口签名工具类

1 import com.alibaba.fastjson.JSON; 2 import com.alibaba.fastjson.serializer.SerializerFeature; 3 import org.springframework.util.StringUtils; 4 5 import java.security.MessageDigest; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 /** 10 * 签名

JDBC的增删改写成一个方法,调用一个工具类

package com.hx.jdbc.connection; import java.sql.Connection; import java.sql.Statement; import com.mysql.jdbc.UpdatableResultSet; import junit.framework.TestCase; public class JDBCText extends TestCase { /** * 创建一个insert,update,delete通用的方法 * @param sq

Hibernate-validate工具类,手动调用校验返回结果

引言:在常见的工程中,一般是在Controller中校验入参,校验入参的方式有多种,这里介绍的使用hibernate-validate来验证,其中分为手动和自动校验,自动校验可以联合spring,使用@Valid注解,以及 BindingResult类来自动收集消息,这里介绍的如何自动的,随时随地的调用工具类,校验指定类或注定属性是否符合规则,将错误封装. 1. 添加 hibernate-validate依赖 <!--hibernate validate--> <dependency&g

java Collections集合工具类

/* Collections是Collection的工具类. 工具类中其中两个比较常用的方法就是: 1,sort(list) . sort(list,自定义比较器) 2,max(list) . max(list,自定义比较器) 3,binarySearch(list,key) //利用折半查找的方法找到list中key元素的索引,存在则返回索引,不存在则返回-(插入点)-1 */ import java.util.*; class CollectionsDemo { public static