java調用http接口的兩種方式

1.get方式

private void httpURLGETCase() {
        String methodUrl = "http://xx.xx.xx.xx:8086/sp-test/usertest/query";
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        String line = null;
        try {
            URL url = new URL(methodUrl + "?mobile=15334567890&name=zhansan");
            connection = (HttpURLConnection) url.openConnection();// 根據URL生成HttpURLConnection
            connection.setRequestMethod("GET");// 默認GET請求
            connection.connect();// 建立TCP連接
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 發送http請求
                StringBuilder result = new StringBuilder();
                // 循環讀取流
                while ((line = reader.readLine()) != null) {
                    result.append(line).append(System.getProperty("line.separator"));// "\n"
                }
                System.out.println(result.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.disconnect();
        }
    }

2.post方式

private static void httpURLPOSTCase() {
        String methodUrl = "http://xxx.xxx.xx.xx:8280/xx/adviserxx ";
        HttpURLConnection connection = null;
        OutputStream dataout = null;
        BufferedReader reader = null;
        String line = null;
        try {
            URL url = new URL(methodUrl);
            connection = (HttpURLConnection) url.openConnection();// 根據URL生成HttpURLConnection
            connection.setDoOutput(true);// 設置是否向connection輸出,因爲這個是post請求,參數要放在http正文內,因此需要設爲true,默認情況下是false
            connection.setDoInput(true); // 設置是否從connection讀入,默認情況下是true;
            connection.setRequestMethod("POST");// 設置請求方式爲post,默認GET請求
            connection.setUseCaches(false);// post請求不能使用緩存設爲false
            connection.setConnectTimeout(3000);// 連接主機的超時時間
            connection.setReadTimeout(3000);// 從主機讀取數據的超時時間
            connection.setInstanceFollowRedirects(true);// 設置該HttpURLConnection實例是否自動執行重定向
            connection.setRequestProperty("connection", "Keep-Alive");// 連接複用
            connection.setRequestProperty("charset", "utf-8");

            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", "Bearer 66cb225f1c3ff0ddfdae31rae2b57488aadfb8b5e7");
            connection.connect();// 建立TCP連接,getOutputStream會隱含的進行connect,所以此處可以不要

            dataout = new DataOutputStream(connection.getOutputStream());// 創建輸入輸出流,用於往連接裏面輸出攜帶的參數
            String body = "[{\"orderNo\":\"44921902\",\"adviser\":\"測試\"}]";
            dataout.write(body.getBytes());
            dataout.flush();
            dataout.close();

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 發送http請求
                StringBuilder result = new StringBuilder();
                // 循環讀取流
                while ((line = reader.readLine()) != null) {
                    result.append(line).append(System.getProperty("line.separator"));//
                }
                System.out.println(result.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.disconnect();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章