1 新建java类,作用是绕过证书用

package cn.smartercampus.core.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager;
public class MyX509TrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return null;
}
}

  

2 https post请求

package cn.smartercampus.core.util;

import javax.net.ssl.*;
import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map; public class HttpsPostUtil {
//添加主机名验证程序类,设置不验证主机
private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}; //添加信任主机
private static void trustAllHosts() {
// 创建不验证证书链的信任管理器 这里使用的是x509证书
TrustManager[] trustAllCerts = new TrustManager[]{new MyX509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
} public void checkClientTrusted(X509Certificate[] chain, String authType) {
} public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
}};
// 安装所有信任的信任管理器
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
//HttpsURLConnection通过SSLSocket来建立与HTTPS的安全连接,SSLSocket对象是由SSLSocketFactory生成的。
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 发送post 数据
* @param urls
* @return
*/
public static String heart(String urls, String param, String contentType, String method) {
StringBuffer sb=new StringBuffer();
DataOutputStream out = null;
BufferedReader responseReader = null;
InputStream in1 = null;
try {
trustAllHosts();
// 创建url资源
URL url = new URL(urls);
// 建立http连接
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
// 设置不用缓存
conn.setUseCaches(false);
// 设置允许输出
conn.setDoOutput(true);
// 设置允许输入
conn.setDoInput(true);
// 设置传递方式
conn.setRequestMethod(method);
//System.out.println(conn.getRequestMethod());
// 设置维持长连接
conn.setRequestProperty("Connection", "Keep-Alive");
// 设置文件字符集:
conn.setRequestProperty("Charset", "UTF-8");
// 转换为字节数组
// byte[] data = (param).getBytes();
// // 设置文件长度
// conn.setRequestProperty("Content-Length", String.valueOf(data.length));
// 设置文件类型:
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Authorization", param);
// 开始连接请求
conn.connect();
out = new DataOutputStream(conn.getOutputStream());
// 写入请求的字符串
out.writeBytes(param);
out.flush(); //System.out.println(conn.getResponseCode()); // 请求返回的状态
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
System.out.println("连接成功");
// 请求返回的数据
in1 = conn.getInputStream();
String readLine;
responseReader = new BufferedReader(new InputStreamReader(in1));
while((readLine=responseReader.readLine()) != null){
sb.append(readLine).append("\n");
}
} else {
System.out.println("error++");
}
} catch (Exception e) { } finally {
try {
if (null != responseReader)
responseReader.close();
if (null != in1)
in1.close();
} catch(Exception e) {}
try {
out.close();
} catch(Exception e) {}
} return sb.toString(); } /**
* 发送post 数据
* @param urls
* @return
*/
public static String sendPost(String urls, String param, String contentType, String method) {
StringBuffer sb=new StringBuffer();
DataOutputStream out = null;
BufferedReader responseReader = null;
InputStream in1 = null;
try {
trustAllHosts();
// 创建url资源
URL url = new URL(urls);
// 建立http连接
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
// 设置不用缓存
conn.setUseCaches(false);
// 设置允许输出
conn.setDoOutput(true);
// 设置允许输入
conn.setDoInput(true);
// 设置传递方式
conn.setRequestMethod(method);
//System.out.println(conn.getRequestMethod());
// 设置维持长连接
conn.setRequestProperty("Connection", "Keep-Alive");
// 设置文件字符集:
conn.setRequestProperty("Charset", "UTF-8");
// 转换为字节数组
// byte[] data = (param).getBytes();
// // 设置文件长度
// conn.setRequestProperty("Content-Length", String.valueOf(data.length));
// 设置文件类型:
conn.setRequestProperty("Content-Type", contentType);
// 开始连接请求
conn.connect();
out = new DataOutputStream(conn.getOutputStream());
// 写入请求的字符串
out.writeBytes(param);
out.flush(); //System.out.println(conn.getResponseCode()); // 请求返回的状态
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
System.out.println("连接成功");
// 请求返回的数据
in1 = conn.getInputStream();
String readLine;
responseReader = new BufferedReader(new InputStreamReader(in1));
while((readLine=responseReader.readLine()) != null){
sb.append(readLine).append("\n");
}
} else {
System.out.println("error++");
}
} catch (Exception e) { } finally {
try {
if (null != responseReader)
responseReader.close();
if (null != in1)
in1.close();
} catch(Exception e) {}
try {
out.close();
} catch(Exception e) {}
} return sb.toString(); } /**
* map转url参数
*/
public static String map2Url(Map<String, String> paramToMap) {
if (null == paramToMap || paramToMap.isEmpty()) {
return null;
}
StringBuffer url = new StringBuffer();
boolean isfist = true;
for (Map.Entry<String, String> entry : paramToMap.entrySet()) {
if (isfist) {
isfist = false;
} else {
url.append("&");
}
url.append(entry.getKey()).append("=");
String value = entry.getValue();
if (!StringUtils.isEmpty(value)) {
url.append(value);
}
}
return url.toString();
} public static void main(String[] args) {
System.out.println("123");
Map<String, String> params = new HashMap<String, String>();
params.put("client_id", "xx.cn");
params.put("client_secret", "UY83SNFJWLU32NJSLJIK");
params.put("code", "40ce7f41d6562e47dbdc33593ddce1b46444cdb93514b3aaced7be90eb51a16f");
params.put("grant_type", "authorization_code");
params.put("redirect_uri", "xxn/index.jsp");
//要发送的POST请求url?Key=Value&amp;Key2=Value2&amp;Key3=Value3的形式
//application/json;charset=UTF-8 application/x-www-form-urlencoded;charset=UTF-8
String sb = sendPost("https://id.pdedu.sh.cn/connect/token",map2Url(params),"application/x-www-form-urlencoded;charset=UTF-8","POST");
//System.out.println(sb); } }

java https post请求并忽略证书,参数放在body中的更多相关文章

  1. java在访问https资源时,忽略证书信任问题 (转)

    java程序在访问https资源时,出现报错sun.security.validator.ValidatorException: PKIX path building failed: sun.secu ...

  2. java https单向认证(忽略认证)并支持http基本认证

    https单向认证(忽略认证)并支持http基本认证, 温馨提示 1,jar包要导入对 2,有匿名类编译要注意 3,欢迎提问,拿走不谢!背景知识 Https访问的相关知识中,主要分为单向验证和双向验证 ...

  3. java遍历http请求request的所有参数实现方法

    方法一: 通过程序遍历http请求的所有参数放到hashmap中,用的时候方便了. 如果参数值有中文,那么需要在程序中添加filter转码,或者在下面程序里,对paramValue转码 Map map ...

  4. java https客户端请求

    String pathname = Test3.class.getResource("/client.jks").getFile(); System.out.println(pat ...

  5. HTTP-java访问https资源时,忽略证书信任问题,代码栗子

    java程序在访问https资源时,出现报错 sun.security.validator.ValidatorException: PKIX path building failed: sun.sec ...

  6. java自动化测试-http请求post

    继上文的get请求http://www.cnblogs.com/xuezhezlr/p/7667995.html的简单讲解后,这篇文章大概说一下我所遇到的主要的post请求以及他的测试代码 上文介绍到 ...

  7. Fiddler怎么可以抓取https的请求包

    对于https的协议是带有安全的功能,所有有一些的https的协议是无法抓到的,所以需要通过设置filler中来对,来使用filler的方式的来抓取到https的请求包,那么如何使用filler中抓取 ...

  8. RestTemplate请求https忽略证书认证

    RestTemplate是Spring提供的用于访问Rest服务的客户端,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率.RestTemplate 默认使用J2SE提供的方式( ...

  9. java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory

    当不需要任何证书访问https时,java中先实现一个MySSLProtocolSocketFactory类忽略证书的信任 package com.tgb.mq.producer.utils; imp ...

随机推荐

  1. 洛谷P2216 理想的正方形(单调队列)

    洛谷P2216 理想的正方形 题目链接 思路: 直接暴力显然不可行,可以发现每一个矩形向右边扩展时是一列一列增加,于是可以想到单调队列,用数组来维护当前每列的最大值.因为行也有限制,所以还要用一个单调 ...

  2. httprunner学习15-运行用例命令行参数详解

    前言 HttpRunner 在命令行中启动测试时,通过指定参数,可实现丰富的测试特性控制. 命令行参数CLI 使用 -h 查看相关命令行参数 hrun -h 参数名称 参数值 参数说明 -h, --h ...

  3. 题解:[HAOI2008]下落的圆盘

    时空限制:1000ms / 128MB 原题链接: 洛谷 bzoj Description 有n个圆盘从天而降,后面落下的可以盖住前面的.求最后形成的封闭区域的周长.看下面这副图, 所有的红 色线条的 ...

  4. Java检查异常、非检查异常、运行时异常、非运行时异常的区别

    Java把所有的非正常情况分为两种:异常(Exception)和错误(Error),它们都继承Throwable父类. Java的异常(Exception和Error)分为检查异常和非检查的异常. 其 ...

  5. DSL的本质:领域构建的半成品

    DSL的本质是使用通用和专用语言构建领域的半成品: 实际上是构建了一个世界观.小宇宙的半成品: 这个半成品包含领域的基本要素.联系方式和基本运行规律: 开发者使用这个半成品平台进行开发能达到事半功倍. ...

  6. NSData、数据结构与数据转换

    数据结构公式:Data_Structure=(D,R): 只要数据元素与数据(组织关系)能够保持:同一个数据(结构)可以在各种存贮形式间进行转换. 字节流或字符串是所有转化的中间节点(中转站).相当于 ...

  7. Aquameta 基于postgresql的web 开发平台

    Aquameta 是一个完全基于pg 开发的web平台 ,目前还在开发中. 当前支持的功能 meta 写入系统信息到pg bundle 基于pg 类似git 的文件系统 filesystem 双向文件 ...

  8. 封装好的observer.js,用于非父子组件传值,直接调用$on和$emit方法

    const eventList = {} const $on = (eventName,callback)=>{ if(!eventList[eventName]){ eventList[eve ...

  9. Xilinx ISE中Synplicity.ucf无法加上去的问题

    在Xilinx ISE中使用Synplify pro进行综合时,有时出现无法将synplicity.ucf添加进工程的问题.这时可以在其它目录下备份synplicity.ucf, 然后使用clean ...

  10. java NIO面试题剖析

    转载:https://mp.weixin.qq.com/s/YIcXaH7AWLJbPjnTUwnlyQ 首先我们分别画图来看看,BIO.NIO.AIO,分别是什么? BIO:传统的网络通讯模型,就是 ...