Java獲取http和https協(xié)議返回的json數(shù)據(jù)
現(xiàn)在很多公司都是將數(shù)據(jù)返回一個json,而且很多第三方接口都是返回json數(shù)據(jù),而且還需要使用到http協(xié)議,http協(xié)議是屬于為加密的協(xié)議,而https協(xié)議需要SSL證書,https是將用戶返回的信息加密處理,然而我們要獲取這些數(shù)據(jù),就需要引入SSL證書?,F(xiàn)在我提供兩個方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。
獲取http協(xié)議的數(shù)據(jù)的方法,如下:
public static JSONObject httpRequest(String requestUrl, String requestMethod) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
// http協(xié)議傳輸
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 設(shè)置請求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 將返回的輸入流轉(zhuǎn)換成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 釋放資源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
獲取https協(xié)議的數(shù)據(jù)的方法,如下:
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 創(chuàng)建SSLContext對象,并使用我們指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 從上述SSLContext對象中得到SSLSocketFactory對象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 設(shè)置請求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 當(dāng)有數(shù)據(jù)需要提交時
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意編碼格式,防止中文亂碼
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 將返回的輸入流轉(zhuǎn)換成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 釋放資源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log.error("Weixin server connection timed out.");
} catch (Exception e) {
log.error("https request error:{}", e);
}
return jsonObject;
}
獲取https協(xié)議的數(shù)據(jù)和獲取http協(xié)議的區(qū)別在于
// 創(chuàng)建SSLContext對象,并使用我們指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 從上述SSLContext對象中得到SSLSocketFactory對象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
大家有更好的方法歡迎留言分享,以上就是本次共享的內(nèi)容 。還有,提示一下,如果復(fù)制中,缺失jar包,請自行下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)在線五子棋對戰(zhàn)游戲(人機(jī)對戰(zhàn))
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實現(xiàn)在線五子棋對戰(zhàn)游戲(人機(jī)對戰(zhàn)),文中的實現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試一下2022-09-09
Spring中的Eureka服務(wù)過期詳細(xì)解析
這篇文章主要介紹了Spring中的Eureka服務(wù)過期詳細(xì)解析,如果有一些服務(wù)過期了,或者宕機(jī)了,就不會調(diào)用shutdown()方法,也不會去發(fā)送請求下線服務(wù)實例,需要的朋友可以參考下2023-11-11
LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情
這篇文章主要介紹了LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情,文章基于Java的相關(guān)資料展開對LeetCode?動態(tài)規(guī)劃的詳細(xì)介紹,需要的小伙伴可以參考一下2022-04-04
詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)
這篇文章主要介紹了詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
如何調(diào)用chatGPT實現(xiàn)代碼機(jī)器人
最近chatGPT也是非常的火爆,相信大家都看到了,現(xiàn)在提供一種Java調(diào)用chatGPT的方法,我們主要通過兩個工具來實現(xiàn),一就是httpclient,二就是hutool,你覺得那種好理解你就用那種即可,今天通過本文給大家分享調(diào)用chatGPT實現(xiàn)代碼機(jī)器人,感興趣的朋友一起看看吧2022-12-12

