JAVA利用HttpClient进行HTTPS接口调用的方法
作者:路常有 发布时间:2023-10-24 16:28:11
标签:java,httpclient
本文介绍了JAVA利用HttpClient进行HTTPS接口调用的方法,分享给大家,具体如下:
1.为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* 用于进行Https请求的HttpClient
* @ClassName: SSLClient
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:42:07
*
*/
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
2.创建一个利用HttpClient发送post请求的工具类
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
* 利用HttpClient进行post请求的工具类
* @ClassName: HttpClientUtil
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:43:38
*
*/
public class HttpClientUtil {
@SuppressWarnings("resource")
public static String doPost(String url,String jsonstr,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
3.测试代码
public static void main(String[] args){
String url = "https://192.168.1.101/xxx";
String jsonStr = "{xxx}";
String httpOrgCreateTestRtn = HttpClientUtil.doPost(url, jsonStr, "utf-8");
}
来源:http://www.cnblogs.com/luchangyou/p/6375166.html


猜你喜欢
- MyBatis-Generator自动生成映射文件生成的方式一共有三种1、使用cmd命令方式生成首先在generator.xml中指定数据库
- Dart实体类格式class CategoryMo { String name; int count;CategoryMo({this.na
- Java中的阻塞队列1. 什么是阻塞队列?阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附加的操作是:在队列为空
- JSON字符串和java对象的互转【json-lib】在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML、JSON等,JSON
- 单选题:(每道题目2分)1. 下列哪个声明是错误的?(B) A. int i=10;B. float f=1.1;&
- 如何使用 Jetpack Compose 创建翻转卡片效果介绍在电子商务和银行应用程序中输入卡信息是很常见的情况。我认为让用户更轻松地处理这
- 本文就是会将数组里面的单词进行倒序排列 例如 how old are you -> you are old how示例程序输出结果:t
- equals函数在基类object中已经定义,源码如下 public boolean equals(Object obj) { return
- 1.面对对象的初步认识1.1什么是面向对象用面向对象的思想来涉及程序,更符合人们对事物的认知,对于大型程序的设计、扩展以及维护都非常友好。1
- 示例1项目结构代码controller中 UserController.javapackage com.example.demo1110.c
- /// <summary> ///
- 对象的读写使用ObjectInputStream和ObjectOutputStream读写对象(序列化与反序列化)。只有字节流没有字符流.类
- SpringBoot使用Commons Logging进行所有内部日志记录,但保留底层日志实现。默认提供了Java Util Logging
- 开发环境JDK1.8 eclipse struts2-2.3.31 1.创建web项目 2.导入struts2核心jar包 3.更改web.
- Word中设置水印时,可加载图片设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水
- 不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录action,就可以实现登录到其他系统。ssh框架项
- 前言在 上一节 Spring解密 - 默认标签的解析 中,重点分析了 Spring 对默认标签是如何解析的,那么本章继续讲解标签解析,着重讲
- Kotlin 面向对象这几天一直在准备考试,实在没有时间,已经过去了这么久,终于要到面向对象了!先看看Kotlin中的类长什么样吧.可以看到
- Spring Boot CLI是Spring Boot项目提供的一个用于快速运行Spring Boot应用的命令行工具,通过结合Groovy
- AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比较