首先是三个返回的实体类

BaseVo.java

package https2;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List; public class BaseVo implements Serializable { private static final long serialVersionUID = 1L; public List<String> getField(Object model) {
List<String> list = new ArrayList<String>(); java.lang.reflect.Method[] method = model.getClass().getDeclaredMethods();// 获取对象所有方法
for (java.lang.reflect.Method m : method) { if (m.getName().startsWith("get")) {// 获取get方法
Object o = null;
try {
o = m.invoke(model);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 执行
if (o == null ) {
list.add("");
} else {
list.add(o.toString());
}
}
}
return list;
}
}

  

PageVo.java

package https2;

public class PageVo extends BaseVo {
/**
*
*/
private static final long serialVersionUID = 1L;
private int current=1;//当前第几页
private int currentNum=1;//当前第几页
private int total;//总数
private int pages;//页数
private int number = 15;//每页多少条
private String column;
private String order; public int getCurrentNum() {
return currentNum;
} public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
this.currentNum = current;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
if(this.total%this.number==0){
this.pages = this.total / this.number;
}else{
this.pages = this.total / this.number + 1;
}
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
} }

  

ResultVo.java

package https2;

public class ResultVo extends BaseVo{
/**
*
*/
private static final long serialVersionUID = 1L; private String code;
private String msg;
private Object data;
private PageVo page; public PageVo getPage() {
return page;
}
public void setPage(PageVo page) {
this.page = page;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
} }

  

然后是一个工具类

package https2;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession; public class HostnameVerifierUtil implements HostnameVerifier { @Override
public boolean verify(String hostname, SSLSession arg1) {
if("localhost".equals(hostname)){
return true;
} else {
return false;
}
} }

  

最后是https请求工具类

package https2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import cn.smartercampus.core.vo.ResultVo; import com.alibaba.fastjson.JSON; public class HttpsPost {
/**
* 获得KeyStore.
* @param keyStorePath
* 密钥库路径
* @param password
* 密码
* @return 密钥库
* @throws Exception
*/
public static KeyStore getKeyStore(String password, String keyStorePath)
throws Exception {
// 实例化密钥库
KeyStore ks = KeyStore.getInstance("JKS");
// 获得密钥库文件流
FileInputStream is = new FileInputStream(keyStorePath);
// 加载密钥库
ks.load(is, password.toCharArray());
// 关闭密钥库文件流
is.close();
return ks;
} /**
* 获得SSLSocketFactory.
* @param password
* 密码
* @param keyStorePath
* 密钥库路径
* @param trustStorePath
* 信任库路径
* @return SSLSocketFactory
* @throws Exception
*/
public static SSLContext getSSLContext(String password,
String keyStorePath, String trustStorePath) throws Exception {
// 实例化密钥库
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
// 获得密钥库
KeyStore keyStore = getKeyStore(password, keyStorePath);
// 初始化密钥工厂
keyManagerFactory.init(keyStore, password.toCharArray()); // 实例化信任库
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// 获得信任库
KeyStore trustStore = getKeyStore(password, trustStorePath);
// 初始化信任库
trustManagerFactory.init(trustStore);
// 实例化SSL上下文
SSLContext ctx = SSLContext.getInstance("TLS");
// 初始化SSL上下文
ctx.init(keyManagerFactory.getKeyManagers(),
trustManagerFactory.getTrustManagers(), null);
// 获得SSLSocketFactory
return ctx;
} /**
* 初始化HttpsURLConnection.
* @param password
* 密码
* @param keyStorePath
* 密钥库路径
* @param trustStorePath
* 信任库路径
* @throws Exception
*/
public static void initHttpsURLConnection(String password,
String keyStorePath, String trustStorePath) throws Exception {
// 声明SSL上下文
SSLContext sslContext = null;
// 实例化主机名验证接口
HostnameVerifierUtil hnv = new HostnameVerifierUtil();
try {
sslContext = getSSLContext(password, keyStorePath, trustStorePath);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
} /**
* 发送请求.
* @param httpsUrl
* 请求的地址
* @param xmlStr
* 请求的数据
*/
public static ResultVo post(String httpsUrl, String xmlStr) {
HttpsURLConnection urlCon = null;
try {
urlCon = (HttpsURLConnection) (new URL(httpsUrl)).openConnection();
urlCon.setDoInput(true);
urlCon.setDoOutput(true);
urlCon.setRequestMethod("POST");
urlCon.setRequestProperty("Content-Length",
String.valueOf(xmlStr.getBytes().length));
urlCon.setUseCaches(false); urlCon.getOutputStream().write(xmlStr.getBytes("UTF-8"));
urlCon.getOutputStream().flush();
urlCon.getOutputStream().close();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlCon.getInputStream()));
String line;
StringBuffer resultStr = new StringBuffer();
while ((line = in.readLine()) != null) {
resultStr.append(line);
}
System.out.println(resultStr.toString());
ResultVo resultVo = JSON.parseObject(resultStr.toString(), ResultVo.class);
return resultVo;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 测试方法.
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 密码
String password = "";
// 密钥库
String keyStorePath = "";
// 信任库
String trustStorePath = "";
// 本地起的https服务
String httpsUrl = "";
// 传输文本
String xmlStr = "";
HttpsPost.initHttpsURLConnection(password, keyStorePath, trustStorePath);
// 发起请求
HttpsPost.post(httpsUrl, xmlStr);
}
}

  

jar 包 fastjson-1.1.27.jar 、log4j-1.2.11.jar

链接:https://pan.baidu.com/s/1UAGUmc7PhUT0Db65YuQMiA
提取码:s70w

带jsk证书,请求https接口的更多相关文章

  1. 通过HttpWebRequest请求https接口

    一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...

  2. 通过证书请求Https站点

    前几天在做与平安银行对接接口,主要是给平安银行推送用户数据(申请贷款的用户),平安银行提供的是https的地址,请求https地址的时候还要发送证书,刚接到这个任务的时候一头雾水,百度上各种所搜,最后 ...

  3. Win7下 httpRequest带证书请求https网站

    常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...

  4. PHP Curl请求Https接口

    在请求http的时候只需要 file_get_contents("http://www.sojson.com/open/api/weather/json.shtml?city=$Positi ...

  5. Vue-cli 本地开发请求https 接口 DEPTH_ZERO_SELF_SIGNED_CERT

    环境:npm run dev 本地开发连接后台的开发环境的接口. 贴上proxyTable 的转发(代理?反向?这个具体叫什么不明白...) proxyTable: { "/api" ...

  6. 转Postman请求Https接口

    转自:https://blog.csdn.net/ONS_cukuyo/article/details/79172242 单向认证 像平常一样访问就行,无需做任何处理,只需要把http://变成htt ...

  7. 请求https接口时报错:Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificat,安装certifi

    如何解决SSL 根证书验错误: 一种解决方法是:verify=False 在session.request 里面: session.post(post_url,data=post_data,heade ...

  8. python 使用requests 请求 https 接口 ,取消警告waring

    response = requests.request("POST", url, timeout=20, data=payload, headers=headers, proxie ...

  9. [Java] 绕过证书验证调 HTTPS 接口时报 “SSLHandshakeException: DHPublicKey does not comply to algorithm constraints”的解决办法

    作者: zyl910 一.缘由 最近有在对接一个无证书的HTTPS接口时,总是收到"SSLHandshakeException: DHPublicKey does not comply to ...

随机推荐

  1. EMQ学习笔记---Clean Session和Retained Message

    MQTT会话(Clean Session)MQTT客户端向服务器发起CONNECT请求时,可以通过’Clean Session’标志设置会话.‘Clean Session’设置为0,表示创建一个持久会 ...

  2. Java并发编程(四):线程安全性

    一.定义 当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程安全的. 二.线程安 ...

  3. PHP写webservice服务端

    1) WebService技术介绍 WebService是一种跨编程语言和跨操作系统平台的远程调用技术.仅仅有通过Web Service,client和server才可以自由的用HTTP进行通信.不论 ...

  4. Haskell示例

    i :: Int i = --add, sub :: Int -> Int -> Int add, sub :: (Num a) => a -> a -> a add a ...

  5. Java中数据库连接的一些方法资料汇总

    Java中Connection方法笔记 http://www.cnblogs.com/bincoding/p/6554954.html ResultSet详解(转)  https://www.cnbl ...

  6. Linux SWAP 深度解读

    概述 本文讨论的swap基于Linux4.4内核代码.Linux内存管理是一套非常复杂的系统,而swap只是其中一个很小的处理逻辑. 希望本文能让读者了解Linux对swap的使用大概是什么样子.阅读 ...

  7. hMailServer之发送附件大小限制

    hMailServer发送附件大小限制有以下几个地方: 1.php配置 参考 .post_max_size = 10M 表单提交最大数据为10M.此项不是限制上传单个文件的大小,而是针对整个表单的提交 ...

  8. binutils工具集之---objdump

    在嵌入式软件开发中,有时需要知道所生成的程序文件中的段信息以分析问题,或者需要查看c语言对应的汇编代码,此时,objdump工具就可以帮大忙了.obj——object  dump:转储. #inclu ...

  9. Error: EACCES: permission denied, symlink

    环境说明 ganiks@ganiks-ubuntu-trusty-64:/ganiks/parse-server$ npm -v 6.5.0 ganiks@ganiks-ubuntu-trusty-6 ...

  10. hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。

    /** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...