Java獲取http和https協(xié)議返回的json數(shù)據(jù)_第1頁
Java獲取http和https協(xié)議返回的json數(shù)據(jù)_第2頁
Java獲取http和https協(xié)議返回的json數(shù)據(jù)_第3頁
Java獲取http和https協(xié)議返回的json數(shù)據(jù)_第4頁
Java獲取http和https協(xié)議返回的json數(shù)據(jù)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、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 證書。 現(xiàn)在我提供兩個方法, 幫助各位 如何獲取 http 和 https 返回的數(shù)據(jù)。 獲取 http 協(xié)議的數(shù)據(jù)的方法,如下: public static JSONObject httpRequest(String requestUrl,

2、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(fal

3、se); / 設(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 buffer

4、edReader = 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.toS

5、tring(); 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 對象,并使用我們指定的 信任管理器初始化Trus

6、tManager tm = new MyX509TrustManager() ;SSLContext sslContext =SSLContext.getInstance(SSL, SunJSSE);/ 從上述 SSLContext 對象中得到 SSLSocketFactory 對象SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url URL(requestUrl);HttpsURLConnection httpUrlConn =(HttpsURLConnection) url.openConnection();httpUr

7、lConn.setSSLSocketFactory(ssf); httpUrlConn.setDoOutput(true);httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false);/ 設(shè)置請求方式( GET/POST )httpUrlConn.setRequestMethod(requestMethod);if (GET.equalsIgnoreCase(requestMethod)httpUrlConn.connect();當有數(shù)據(jù)需要提交時if (null != outputStr) OutputStream output

8、Stream = httpUrlConn.getOutputStream();/ 注意編碼格式,防止中文亂碼outputStream.write(outputStr.getBytes(UTF-8);new/outputStream.close();/ 將返回的輸入流轉(zhuǎn)換成字符InputStream inputStream =httpUrlConn.getInputStream();InputStreamReader inputStreamReader = new InputStreamReader(inputStream, utf-8);BufferedReader bufferedReade

9、r = 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();

10、catch (ConnectException ce) log.error(Weixin server connection timedout.); catch (Exception e) log.error(https request error:, e);return jsonObject;獲取 https 協(xié)議的數(shù)據(jù)和獲取 http 協(xié)議的區(qū)別在于 / 創(chuàng)建 SSLContext 對象,并使用我們指定的信任管理器初始化TrustManager tm = newMyX509TrustManager() ;SSLContext sslContext =SSLContext.getInstance(SSL, SunJSSE);/ 從上述 SSLContext 對象中得到SSLSocketFactory 對象SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl);HttpsURLConnection httpUrlConn =(Http

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論