第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

javaweb、jsp中調(diào)用外部的http接口

javaweb、jsp中調(diào)用外部的http接口

搖曳的薔薇 2019-03-14 10:10:14
javaweb、jsp中調(diào)用外部的http接口 
查看完整描述

2 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊


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

public static String sendPostUrl(String url, String param, String charset) {

 

    PrintWriter out = null;

    BufferedReader in = null;

    String result = "";

    try {

      URL realUrl = new URL(url);

      // 打開和URL之間的連接

      URLConnection conn = realUrl.openConnection();

      // 設(shè)置通用的請(qǐng)求屬性

      conn.setRequestProperty("accept", "*/*");

      conn.setRequestProperty("connection", "Keep-Alive");

      conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

      // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行

      conn.setDoOutput(true);

      conn.setDoInput(true);

      // 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流

      out = new PrintWriter(conn.getOutputStream());

      // 發(fā)送請(qǐng)求參數(shù)

      out.print(param);

      // flush輸出流的緩沖

      out.flush();

      // 定義BufferedReader輸入流來讀取URL的響應(yīng)

      in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));

      String line;

      while ((line = in.readLine()) != null) {

        result += line;

      }

    } catch (Exception e) {

      System.out.println("發(fā)送 POST 請(qǐng)求出現(xiàn)異常!" + e);

      e.printStackTrace();

    }

    // 使用finally塊來關(guān)閉輸出流、輸入流

    finally {

      try {

        if (out != null) {

          out.close();

        }

        if (in != null) {

          in.close();

        }

      } catch (IOException ex) {

        ex.printStackTrace();

      }

    }

    return result;

  }



查看完整回答
反對(duì) 回復(fù) 2019-03-16
  • 2 回答
  • 0 關(guān)注
  • 2809 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)