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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)