当前位置: 首页 > news >正文

java调用第三方接口工具类 (HttpClientUtils.java)

1. 依赖

<!--httpclient-->
<dependency><groupId>commons-httpclient</groupId><artifactId>commons-httpclient</artifactId><version>3.1</version>
</dependency><!-- 阿里JSON解析器 -->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.80</version>
</dependency>

2. HttpClientUtils.java

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** 调用第三方接口工具类*/
public class HttpClientUtils {/*** 带参数的get请求* @param url* @param param* @return String*/public static String doGet(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {// 创建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/*** 不带参数的get请求* @param url* @return String*/public static String doGet(String url) {return doGet(url, null);}/*** 带参数的post请求* @param url* @param param* @return String*/public static String doPost(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建参数列表if (param != null) {List<NameValuePair> paramList = new ArrayList<>();for (String key : param.keySet()) {paramList.add(new BasicNameValuePair(key, param.get(key)));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, Consts.UTF_8);httpPost.setEntity(entity);}// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/*** 不带参数的post请求* @param url* @return String*/public static String doPost(String url) {return doPost(url, null);}/*** 传送json类型的post请求* @param url* @param json* @return String*/public static String doPostJson(String url, String json) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建请求内容StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}
}

3. 字符串转Json
// json
JSONObject json = JSONObject.parseObject(result);// 获取openid
String openid = json.getString("openid");// TODO

http://www.lryc.cn/news/118218.html

相关文章:

  • f1tenth仿真设置
  • Technical debt (技术负债 / 技术债)
  • 【MATLAB第67期】# 源码分享 | 基于MATLAB的morris全局敏感性分析
  • ruby send call 的简单使用
  • 24聊城大学823软件工程考研
  • 勘探开发人工智能技术:机器学习(3)
  • 定制 ChatGPT 以满足您的需求 自定义说明
  • taro h5列表拖拽排序 --- sortablejs 和 react-sortable-hoc
  • Linux的shell脚本常用命令
  • 使用自己的数据集预加载 Elasticsearch
  • 机器视觉赛道持续火热,深眸科技坚持工业AI视觉切入更多应用领域
  • MyBatis操作数据库常见用法总结2
  • 基于SpringBoot+LayUI的宿舍管理系统 001
  • C语言笔记7
  • Centos更换网卡名称为eth0
  • 【Express.js】软件测试
  • TCP三次握手、四次握手过程,以及原因分析
  • OceanBase X Flink 基于原生分布式数据库构建实时计算解决方案
  • 600V EasyPIM™ IGBT模块FB30R06W1E3、FB20R06W1E3B11、FB20R06W1E3降低了系统成本和损耗,可满足高能效要求。
  • form 表单恢复初始数据
  • MySQL—索引
  • Android图形-合成与显示-概论
  • Swift 5 数组如何获取集合的索引和对应的元素值
  • 计算 Nginx 日志的PV和UV
  • Spring中常用的注解
  • Plugin 插件
  • Structure needs cleaning fsimage文件系统损坏修复
  • MATLAB|信号处理的Simulink搭建与研究
  • LinuxC编程——线程
  • 使用fetch调用fastapi接口(post)的实例