各位童鞋大家好,向来简单粗暴的铁柱兄给大家来玩一手套娃模式来组Json数据,不说别的,无脑套。

  当然,这一手比较适合临场用一下,若长期用的话建议搞一套适用的框架,只管set就好了。话不多说开始上课。

  套娃模式这个顾名思义啊,就是一层一层的往里面套就好了,特舒服。先上一手代码:

发送https请求的代码我是随便搜的,这一套如果对你适用的话就直接复制过去了,套娃这套代码对发什么请求都无所谓,只要对方要求的是json格式的。

package test;

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; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject; public class SSLClient extends DefaultHttpClient { public SSLClient() throws Exception {//网上搜的发https的方法
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));
}
public static String doPost(String url, String map, String charset) {
org.apache.http.client.HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity stringEntity = new StringEntity(map);
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
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;
}
private static String url = "https://xx.xx.xx.xxx:xxxx/xxxx/xxxx";//填写需要发送请求的地址 https后面跟ip跟端口跟地址
private static String charset = "utf-8"; public static void main(String[] args) { /**
* new几个JSONObject出来作为套筒
* 需要几层就new几个
*/
JSONObject json=new JSONObject();//最大的套筒
JSONObject json1=new JSONObject();
JSONObject json2=new JSONObject();
//json1、json2作为第二层套筒 这些数据放自己需要的即可
json1.put("txnCode", "GWS00004");
json1.put("reqDate", "20201102");
json1.put("reqTime", "101532");
json1.put("channelId", " stockapp");
json1.put("traceNo", "9e4124f5b1c145c18b698fb7d5628002");
json.put("header", json1);
json2.put("ciNo", "8000001397");
json.put("body",json2 );
String encryptStr = json.toString();
System.out.println("encryptStr:" + encryptStr);
String httpOrgCreateTestRtn = doPost(url, encryptStr, charset);//丢去发送刚组的数据
System.out.println("result:" + httpOrgCreateTestRtn);//返回数据
}
}

  组起来其实是很容易的,有了思路随便套,随便多少层,随意套。

encryptStr:{"header":{"txnCode":"GWS00004","reqDate":"20201102","traceNo":"9e4124f5b1c145c18b698fb7d5628002","reqTime":"101532","channelId":"  stockapp"},"body":{"ciNo":"8000001397"}}
result:{"header":{"txnCode":"GWS00004","resDate":"20201102","resTime":"051127","retCode":"DD6010","errMsg":"DD6010","traceNo":""}}

  

  这套方法也适用Map,解释啥的我一如既往的丢注释里了,有不明白的地方欢迎提问。谢谢

简单粗暴套娃模式组json发送https请求的更多相关文章

  1. 【转载】JMeter学习(三十六)发送HTTPS请求

    Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程中需要进行登录操作,而登录请求是HTTPS协议.这就需要对jmeter进行设置. ...

  2. JMeter学习(三十六)发送HTTPS请求(转载)

    转载自 http://www.cnblogs.com/yangxia-test Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程 ...

  3. Web Server 使用WebClient 发送https请求 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

    使用WebClient 发送https请求 使用WebClient发送请求时,并且是以https协议: WebClient webClient = new WebClient(); string re ...

  4. 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL

    这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...

  5. 【传输协议】发送https请求,由于客户端jdk版本过高,服务端版本低。导致异常:javax.net.ssl.SSLHandshakeException: Server chose SSLv3, but that protocol version is not enabled or not supported by the client.

    本地环境jdk为1.8,服务器使用jdk版本未知.但发送https请求,抛出如下异常,解决方案. 一:发送异常内容如下 javax.net.ssl.SSLHandshakeException: Ser ...

  6. python2/3 发送https请求时,告警关闭方法

    问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)情况下,控制台会输出以下错误: InsecureRequestWarning: Unverifi ...

  7. requests发送HTTPS请求(处理SSL证书验证)

    1.SSL是什么,为什么发送HTTPS请求时需要证书验证? 1.1 SSL:安全套接字层.是为了解决HTTP协议是明文,避免传输的数据被窃取,篡改,劫持等. 1.2 TSL:Transport Lay ...

  8. .Net Core 发送https请求/.net core 调用数字证书 使用X509Certificate2

    .Net Core 发送https请求 .net core 调用数字证书 使用X509Certificate2 .NET下面的 .netfromwork使用和asp.net core下使用方式不一样 ...

  9. Python常见问题 - python3 使用requests发送HTTPS请求报certificate verify failed 错误

    当你使用 requests 发送HTTPS请求时 requests.get(url, parmas=parmas, headers=header, cookies=cookie) 出现了以下错误 HT ...

随机推荐

  1. 【Linux】一些文件限制配置

    linux资源限制配置文件是/etc/security/limits.conf:限制用户进程的数量对于linux系统的稳定性非常重要. limits.conf文件限制着用户可以使用的最大文件数,最大线 ...

  2. 【CSP2019-J】游记

    看我朋友们的博客里面都写了游记,我也来凑个热闹(雾) day1# 介于是\(CSP-J\),我们是比赛当天走的,上午卡点到.一路上不允许玩游戏,于是就在路上看了一路的鬼畜视频,然后看了看对拍的板子(然 ...

  3. 证明RSA算法在明文和公私钥中N不互质情况下仍然成立

    关于RSA的基础过程介绍 下文中的 k 代表自然数常数,不同句子,公式中不一定代表同一个数 之前接触RSA,没有过多的思考证明过程,今天有感而发,推到了一遍 假设公钥 (e, N) , 私钥 (d, ...

  4. docker容器命令1

    docker容器命令 新建并启动容器命令 docker run INAME(镜像名字) 语法:docker run [OPTIONS] INAME [COMMAND] 例子:docker run -i ...

  5. nrf528xx bootloader 模块介绍

    1. bootloader 的基本功能: 启动应用 几个应用之间切换 初始化外设 nordic nrf52xxx的bootloader主要功能用来做DFU, 可以通过HCI, UART 或BLE通信的 ...

  6. 高效的后端API开发模板-illuminant

    概要 整体介绍 补充说明 Q. 有了 prisma 自动生成的 graphql 接口, 为什么还要 rest 接口和 websocket 接口? Q. 为什么要通过 illuminant 加一层反向代 ...

  7. 多测师讲解html _有序列表005_高级讲师肖sir

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>有 ...

  8. 多测师讲解python __for 循环___高级讲师肖sir

    横向输出 1.遍历字符串 2.遍历列表 3.遍历元组 方法一: 方法二: 方法三: #循环字典:方法一# dict1={"name":"zhihao",&quo ...

  9. 三门峡6378.7939(薇)xiaojie:三门峡哪里有xiaomei

    三门峡哪里有小姐服务大保健[微信:6378.7939倩儿小妹[三门峡叫小姐服务√o服务微信:6378.7939倩儿小妹[三门峡叫小姐服务][十微信:6378.7939倩儿小妹][三门峡叫小姐包夜服务] ...

  10. ip地址和网络端口总结

    ip地址 ip地址默认指ipv4地址,用4个字节表示,转换为点分10进制,可以表达范围0.0.0.0到255.255.255.255的地址,大约为42.95亿个地址.互联网编号分配机构(IANA,In ...