文章目录
- 一、客户端实现
- 1. HttpsApiUtils +测试方法
- 2. 返回报文监控
- 二、服务端实现
- 2.1. 配置SSL 实现HTTPS
- 2.2. 添加post接口方法
- 2.3. 服务端监控
- 三、进阶测试
- 3.1. 客户端发送对象
- 3.2. 服务端监控
- 3.3. 客户端解析返回报文
一、客户端实现
声明:不用引入任何第三方jar包,只需要安装JDK即可,目前版本适配1.6及以上版本
1. HttpsApiUtils +测试方法
package com.gblfy.util;import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;/*** 实现HTTPS协议POST请求JSON报文** @author gblfy* @date 2020-06-19*/public class HttpsApiUtil {private static class TrustAnyTrustManager implements X509TrustManager {// 该方法检查客户端的证书,若不信任该证书则抛出异常。由于我们不需要对客户端进行认证,因此我们只需要执行默认的信任管理器的这个方法。// JSSE中,默认的信任管理器类为TrustManager。@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType)throws CertificateException {}/** 该方法检查服务器的证书,若不信任该证书同样抛出异常。通过自己实现该方法,可以使之信任我们指定的任何证书。* 在实现该方法时,也可以简单的不做任何处理, 即一个空的函数体,由于不会抛出异常,它就会信任任何证书。(non-Javadoc)*/@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType)throws CertificateException {}// 返回受信任的X509证书数组。@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[]{};}}private static class TrustAnyHostnameVerifier implements HostnameVerifier {@Overridepublic boolean verify(String hostname, SSLSession session) {return true;}}/*** post方式请求服务器(https协议)** @param url 求地址* @param content 参数* @param charset 编码* @return* @throws NoSuchAlgorithmException* @throws KeyManagementException* @throws IOException*/public static byte[] sendJsonToHttpsPost(String url, String content,String charset) throws NoSuchAlgorithmException,KeyManagementException, IOException {/** 类HttpsURLConnection似乎并没有提供方法设置信任管理器。其实,* HttpsURLConnection通过SSLSocket来建立与HTTPS的安全连接* ,SSLSocket对象是由SSLSocketFactory生成的。* HttpsURLConnection提供了方法setSSLSocketFactory* (SSLSocketFactory)设置它使用的SSLSocketFactory对象。* SSLSocketFactory通过SSLContext对象来获得,在初始化SSLContext对象时,可指定信任管理器对象。*/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.setSSLSocketFactory(sc.getSocketFactory());conn.setHostnameVerifier(new TrustAnyHostnameVerifier());conn.setDoOutput(true);// 设置请求头conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");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();return outStream.toByteArray();}return null;}public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, IOException {String url = "https://127.0.0.1:8888/postToJson";byte[] bytes = sendJsonToHttpsPost(url, "{\"name\":\"ly\"}", "utf-8");System.out.println("响应报文:"+new String(bytes));}
}
2. 返回报文监控
解析
通过上述方式可以得到收发方法post()。传入字符串,返回字节数组。
传入的字符串处理:
JSONObject rspObj = new JSONObject();
rspObj.put("arg1", "aaa");
rspObj.put("arg2", "bbb");
//...
String reqStr = rspObj.toString();
System.out.println("请求json:\n" + reqStr);
返回的字节数组转字符串:
byte[] result = post(reqUrl,reqStr,"utf-8");
if(result != null){String rspStr = new String(result);System.out.println("响应json:\n" + rspStr);
}
二、服务端实现
2.1. 配置SSL 实现HTTPS
Spring Boot 配置SSL 实现HTTPS
2.2. 添加post接口方法
package com.gblfy.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
@RequestMapping
public class ViewControlller {@RequestMapping(value = "/postToJson", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")@ResponseBodypublic String postToJson(@RequestBody String json) {System.out.println("https服务端接收到报文:" + json);return json;}
}
2.3. 服务端监控
三、进阶测试
3.1. 客户端发送对象
package com.gblfy.pojo;import java.io.Serializable;public class User implements Serializable {private String username;private Integer age;private String passwd;public User(String username, Integer age, String passwd) {this.username = username;this.age = age;this.passwd = passwd;}public User() {}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getPasswd() {return passwd;}public void setPasswd(String passwd) {this.passwd = passwd;}
}
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, IOException, JSONException {//1. 模拟对象发送User user = new User();user.setUsername("gblfy");user.setAge(28);user.setPasswd("123456");//2. 配置发送urlString url = "https://127.0.0.1:8888/postToJson";//3.发送前将对象转json处理JSONObject jsonObject = new JSONObject(user);String reqXml = jsonObject.toString();//4.向指定url发送json格式的https协议报文byte[] bytes = sendJsonToHttpsPost(url, reqXml, "utf-8");//5.对返回的报文解析String resXml = new String(bytes);JSONObject jsonObj = new JSONObject(resXml);//6.从解析的报文中获取指定的值System.out.println("服务端返回报文:" + jsonObj.getString("username"));System.out.println("服务端返回报文:" + jsonObj.getInt("age"));System.out.println("服务端返回报文:" + jsonObj.getString("passwd"));}
3.2. 服务端监控
https服务端接收到报文:{"passwd":"123456","age":28,"username":"gblfy"}
3.3. 客户端解析返回报文
服务端返回报文:gblfy
服务端返回报文:28
服务端返回报文:123456