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代码在浏览器里发送请求可以 ...
随机推荐
- Vue.use初探
Vue.use 问题 相信很多人在用Vue使用别人的组件时,会用到 Vue.use(). 例如:Vue.use(VueRouter).Vue.use(MintUI). 但是用 axios时,就不需要用 ...
- Note/Solution - 转置原理 & 多点求值
\[\newcommand{\vct}[1]{\boldsymbol{#1}} \newcommand{\mat}[1]{\begin{bmatrix}#1\end{bmatrix}} \newcom ...
- Solution -「51nod 1514」美妙的序列
\(\mathcal{Description}\) Link. 称排列 \(\{p_n\}\) 美妙,当且仅当 \((\forall i\in[1,n))(\max_{j\in[1,i]}\{ ...
- Hive之同比环比的计算
Hive系列文章 Hive表的基本操作 Hive中的集合数据类型 Hive动态分区详解 hive中orc格式表的数据导入 Java通过jdbc连接hive 通过HiveServer2访问Hive Sp ...
- 使用docker部署awx-1.7.1.0(ansible图形化界面)
文章目录 关于环境 下载awx 下载安装所需依赖 安装docker-compose 配置inventory文件 出现的报错 TASK [local_docker : Run migrations in ...
- tip6:idea 开发工具使用
使用idea开发工具过程中,各种个性化设置或快捷方式使用汇总 1.设置默认maven为本地 2.编写代码时提供完整的参数提示信息 3.编辑器列模式 使用alt+鼠标左键,鼠标下移即可.使用版本idea ...
- [LeetCode]1389. 按既定顺序创建目标数组
给你两个整数数组 nums 和 index.你需要按照以下规则创建目标数组: 目标数组 target 最初为空. 按从左到右的顺序依次读取 nums[i] 和 index[i],在 target 数组 ...
- 设计DFA接受{0,1}上的字符串ω,且ω是3倍数的二进制表示
DFA设计 设计DFA接受{0,1}上的字符串ω,且ω是3倍数的二进制表示 先叙述下思路: 要想证明某数是3的倍数可以让其除以3看余数是否为零即可,现在我们的问题就是如何计算一串二进制数除以3所得的余 ...
- 一个杂项PDF
不是很难,但是就是比较考虑个人的细心程度,下载压缩包解压得到一个pdf文件,直接打开发现没有什么隐藏的信息,准换成word也是,没有什么东西,使用winhex打开看一下,一开始是没有注意到什么的,这里 ...
- System.Console.WriteLine() 调用原理
1.System.Console.WriteLine(类的实例)默认调用类的Tostring()方法.如果自定义的新类未override ToString()方法.那么调用Object.ToStrin ...