一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java獲取http和https協(xié)議返回的json數(shù)據(jù)

Java獲取http和https協(xié)議返回的json數(shù)據(jù)

2020-07-26 14:26java_阿杰 Java教程

本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個方法,幫助各位如何獲取http和https返回的數(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ù)的方法,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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ù)的方法,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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ū)別在于

?
1
2
3
4
5
6
7
8
9
10
// 創(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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/wangtj-19/p/5889056.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩一级片在线免费观看 | 免费波多野结衣庭教师 | 国产精品日韩在线观看 | 欧洲另类一二三四区 | 13日本xxxxxxxxx18 1313午夜精品久久午夜片 | 欧美日韩视频在线成人 | 好大好硬好长好爽a网站 | 波多野结衣在线免费观看 | 成人国产网站v片免费观看 成人国产精品视频 | 色琪琪原网站亚洲香蕉 | 青草免费在线 | 国产一级片免费视频 | 国产精品美女福利视频免费专区 | 国产成人免费在线观看 | 精品香蕉99久久久久网站 | 日韩色在线观看 | kuaibo成人播放器 | 男男调教打屁股 | 国产一级特黄在线播放 | 日本韩国无矿砖码 | 亚洲视频一 | 国产尤物精品视频 | s0e一923春菜花在线播放 | 日本一区视频 | 99热这里只有精品在线 | 奇米影视7777久久精品 | 大妹子最新视频在线观看 | 免费黄色片网站 | 国产主播福利在线观看 | 四虎永久免费在线观看 | 天天夜夜草草久久伊人天堂 | 欧美一级视频在线高清观看 | 色综合网天天综合色中文男男 | 91色在线观看国产 | 哇嘎在线精品视频在线观看 | 久久精品中文闷骚内射 | 欧美vpswindowssex 欧美va在线高清 | chinses台湾男同志hd | aaa黄色| 国产成人一区二区三区视频免费蜜 | 天作谜案免费完整版在线观看 |