二、java发送https的各类请求
导航
java开发中需要调用其他服务的对外提供的https请求,上一篇写的是调用http,处理方式不太一样,可以参考如下代码:
注:调用的主类比较简单就不写了。
pom.xml

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency> <dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>

HttpsUtil.class类
package cn.cas.xjipc.yjsapplication.unit; import org.apache.commons.httpclient.util.HttpURLConnection;
import org.springframework.stereotype.Component; import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.*; @Component
public class HttpsUtil { private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
} private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
} /**
* post方式请求服务器(https协议)
*
* @param url 请求地址
* @param content 参数
* @param charset 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public String post(String url, String content, String charset)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
String result = "";
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
new java.security.SecureRandom()); URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(content.getBytes(charset));
// 刷新、关闭
out.flush();
out.close();
InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
byte[] array = outStream.toByteArray();
result = new String(array, "utf-8");
return result;
}
return null;
} /**
* put方式请求服务器(https协议)
*
* @param url 请求地址
* @param content 参数
* @param token 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public String put(String url, String content, String token)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
String result = "";
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
new java.security.SecureRandom()); URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestMethod("PUT");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
//conn.setRequestProperty("Authorization", "xxxxx" + token);
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
//out.write(content.getBytes("UTF8"));
// 刷新、关闭
out.flush();
out.close();
InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close(); byte[] array = outStream.toByteArray();
result = new String(array, "utf-8");
return result;
}
return null;
} /**
* get方式请求服务器(https协议)
*
* @param url 请求地址
* @param content 参数
* @param token 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
*/
public String get(String url, String content, String token)
throws NoSuchAlgorithmException, KeyManagementException,
IOException {
String result = "";
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
new java.security.SecureRandom()); URL httpUrl = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) httpUrl.openConnection();
conn.setRequestMethod("GET");
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
//conn.setRequestProperty("Authorization", "xxxxxx" + token);
conn.connect();
//get方法与post方法除了conn.setRequestMethod("GET")这句不一样外,关于DataOutputStream out的几行一定要删除,否则就会报405的错误 InputStream is = conn.getInputStream();
if (is != null) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close(); byte[] array = outStream.toByteArray();
result = new String(array, "utf-8");
return result;
} return null;
} }
推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
二、java发送https的各类请求的更多相关文章
- Java发送HTTPS请求
前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...
- Java 发送 Https 请求工具类 (兼容http)
依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...
- Java发送http get/post请求,调用接口/方法
由于项目中要用,所以找了一些资料,整理下来. GitHub地址: https://github.com/iamyong 转自:http://blog.csdn.net/capmiachael/a ...
- JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理
java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...
- Java 发送http GET/POST请求
最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途.轻量极的HTTP客户端,使 ...
- Java发送get及post请求工具方法
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- JAVA发送http get/post请求,调用http接口、方法
import java.io.BufferedReader; import java.io.IOException;import java.io.InputStream; import java.io ...
- Java 发送Get和Post请求
package com.htpt.superviseServices.dm.util; import java.io.BufferedReader; import java.io.IOExceptio ...
- java的https的get请求
package com.wl.webservice; import java.io.InputStream; import java.net.HttpURLConnection; import jav ...
- java发送GET和post请求
package com.baqingshe.bjs.util; import java.io.BufferedReader; import java.io.IOException; import ja ...
随机推荐
- 高效的 Json 解析框架 kotlinx.serialization
一.引出问题 你是否有在使用 Gson 序列化对象时,见到如下异常: Abstract classes can't be instantiated! Register an InstanceCreat ...
- 吉特日化MES & SQL Server 无法执行数据库脚本
打开服务器打算远程执行一下脚本,突发发现数据库无法执行脚本,就想着是不是安装了 海康AGV 控制系统的问题导致,问题如下: 问题截图如下: 报错信息如下: ====================== ...
- [ABC268G] Random Student ID
Problem Statement Takahashi Elementary School has $N$ new students. For $i = 1, 2, \ldots, N$, the n ...
- 技术Leader:像李云龙一样打造学习型团队
今天跟大家分享一下怎么样构建一个学习型的团队. 首先对于计算机行业而言,不明而喻,我们要接受的东西真的太多了.我们接触的信息和变化也太多了.如果只是因循守旧,排斥新东西,那么我们被时代淘汰只是个时间问 ...
- 使用 VS 2019 将 c# 生成 DLL 动态链接库文件
主要步骤: ChatGPT 的回答: 你可以尝试使用 Visual Studio 创建一个类库项目,然后将你写的两个类添加到该项目中,并进行编译,最终生成 DLL 文件.具体步骤如下: 打开 Visu ...
- zookeeper JavaApi 创建节点
import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import ...
- 9 "网址"--URI
目录 URI和URL URI详细介绍 URI的组成 URI的查询参数 URI的编码 疑问 URI和URL URI:统一资源标识符(Uniform Resource Identifier) 有两种形式: ...
- OkHttp3发送http请求
导入依赖 <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> <dependency> ...
- 三维GIS渲染引擎盘点,以Cesium为核心的拓展优化
目前,以Cesium为核心的各类产品繁多,本文将挑选一些以Cesium为核心的软件案例,为大家进行介绍. 1. CesiumJS CesiumJS相信凡是GIS行业相关人员都特别熟悉了,CesiumJ ...
- Javascript Ajax总结——其他跨域技术之服务器发送事件SSE
SSE(server-Sent Events,服务器发送事件)是围绕只读Comet交互推出的API或者模式.SSE API创建到服务器的单向连接,服务器通过这个连接可以发送任意数量的数据.服务器响应的 ...