java的https的get请求
package com.wl.webservice; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
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.SSLSocketFactory;
import javax.net.ssl.X509TrustManager; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ricoh.rapp.ezcx.iwbservice.util.Constant;
import com.ricoh.rapp.ezcx.iwbservice.util.Utils; public class HttpsRequest { private final Logger logger = LoggerFactory.getLogger(HttpsRequest.class); /**
* 处理https GET/POST请求
* @param requestUrl 请求地址
* @param requestMethod 请求方法
* @param requestStr 请求参数
* @return
*/
public JSONObject httpsRequest(String requestUrl, String requestMethod, String requestStr) {
logger.info("req---->:" + requestMethod + requestStr);
HttpsURLConnection httpsConnection = null;
try {
// 创建SSLContext
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] tm = { new TrustManager() };
// 初始化
sslContext.init(null, tm, new java.security.SecureRandom()); // 获取SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
HostnameVerifier HostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String var1, SSLSession var2) {
return true;
} }; URL url = new URL(requestUrl);
httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.setDoOutput(false);
httpsConnection.setDoInput(true);
httpsConnection.setConnectTimeout("3 * 1000");
httpsConnection.setReadTimeout("15 * 1000");
httpsConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpsConnection.setRequestProperty("Charset", "UTF-8");
httpsConnection.setRequestProperty("Authorization", "Basic aXdidXNlcjp0ZXN0MDAwMA==");
httpsConnection.setRequestProperty("User-Agent", "Client identifier");
httpsConnection.setRequestMethod("GET");
/*
* httpsConnection.setUseCaches(false);
* httpsConnection.setRequestMethod(requestMethod);
*/
// 设置当前实例使用的SSLSoctetFactory
httpsConnection.setSSLSocketFactory(ssf);
httpsConnection.setHostnameVerifier(HostnameVerifier);
httpsConnection.connect();
// 往服务器端写内容
// 读取服务器端返回的内容
InputStream inputStream = httpsConnection.getInputStream();
if (httpsConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
logger.error("connect ezcs service failed: " + httpsConnection.getResponseCode());
JSONObject responseJson = new JSONObject();
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
return responseJson;
}
String response = Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
logger.debug("response from ezcs service: " + response);
JSONObject responseJson = JSON.parseObject(response);
return responseJson;
} catch (Exception e) {
e.printStackTrace();
logger.debug("connect local ezcs service exception: " + e.getMessage());
JSONObject responseJson = new JSONObject();
if (e instanceof SocketTimeoutException || e instanceof SocketException) {
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
} else {
responseJson.put(ResponseKey.KEY_RESULT,
com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);
}
return responseJson;
} finally {
if (httpsConnection != null) {
httpsConnection.disconnect();
}
}
} class TrustManager implements X509TrustManager { @Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
} } public static class ResponseKey {
public static final String KEY_RESULT = "result";
public static final String KEY_REASON = "reason";
public static final String KEY_DATA = "data";
public static final String KEY_EXTRA = "extra";
} }
java的https的get请求的更多相关文章
- java 实现https请求
java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从 ...
- Java发送HTTPS请求
前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...
- Java 发送 Https 请求工具类 (兼容http)
依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- HTTPS那些事 用java实现HTTPS工作原理
HTTPS那些事 用java实现HTTPS工作原理 博客分类: java历险 今天被问到关于https原理的问题,结果由于知识掌握不牢靠,停留于表面,很多细节都无法回答清楚,于是决定把https的 ...
- Java访问HTTPS时证书验证问题
为了尽可能避免安全问题,公司的很多系统服务都逐步https化,虽然开始过程会遇到各种问题,但趋势不改.最完美的https应用是能实现双向认证,客户端用私钥签名用服务端公钥加密,服务端用私钥签名客户端都 ...
- java实现https免证书认证
java实现https免证书认证 解决方法: 1.下载两个包,httpclient-4.2.jar和httpcore-4.2.jar,复制以下代码就可使用. 2.调用类代码: String htt ...
- 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析
前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...
- 如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查
我们使用Java的RestTemplate或者Apache的HTTPClient编程的时候,经常遇到需要跟踪Java 代码发送的HTTP请求明细的情况.和javascript代码在浏览器里发送请求可以 ...
随机推荐
- 大话PHP设计模式笔记
针对PHP的设计模式进行总结记录. 顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百度云下载,这样以后你们开发就可以直接翻笔记不用百度搜那么麻 ...
- Note - 千年食谱颂
其实是兔子收集的各种下饭操作与名菜食谱.( 零·策略篇 多校 NOIP 2021.11.05: 这个真的是,我每次打毛毛虫剖分都是 rush 状态 qwq.像这种 已知代码难度大.不便于调试的 ...
- 我们一起来学Shell - shell的函数
文章目录 定义函数 执行不带参数的函数 执行带参数的函数 函数的执行总结 我们一起来学Shell - 初识shell 我们一起来学Shell - shell的变量 我们一起来学Shell - shel ...
- CentOS 7 下升级OpenSSH 7.4p1到OpenSSH 8.4p1
文章目录 一.环境介绍 二.安装配置telnet 2.1.安装telnet-server 2.2.配置telnet 2.3.配置telnet登录的终端类型 2.4.启动telnet服务 三.切换登录方 ...
- linux 运维工程师如何降低工作难度
文章目录 1.Linux "优化" 2.git "优化" 3.mysql "优化" 4.kubernetes "优化" ...
- MyBatis功能点二:plugins插件使用
MyBatis自定义插件使用步骤(已有pojo及mapper的基础上) 一.自定义插件,实现Interceptor接口 二.核心配置文件sqlMapConfig.xml文件增加插件相关内容 测试 测试 ...
- CSS代码示例-背景属性(background)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JSP中引入JQuery和Layer,浏览器控制台报错404
路径没有写错,文件也存在为什么会报404呢?,解决方法是将layer文件夹使用source的方式 解决办法: 这时候你会发现layer文件夹变成了蓝色,重启一次服务器,页面中就没有报404异常了 总结 ...
- 听说:分布式ID不能全局递增?
大家好,我是[架构摆渡人],一只十年的程序猿.这是实践经验系列的第十一篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果有收获,还请分享给更多的朋友. 前面有篇文章我们讲到用时间来代替自增I ...
- js和C# 编码 解码
C#中对URL编码的方法... 编码:Server.UrlEncode(string) 解码:Server.UrlDecode(string) HttpUtility.UrlEncode(string ...