1. 程式人生 > 實用技巧 >Java實現Http請求

Java實現Http請求

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @title: HttpClient類實現Get、Post方法
 * @author: 獵手部落
 * @date: 2020年11月18日 16:00
 * @description: 備忘
 
*/ public class HttpClient { //GET方法 public static String doGet(String httpurl) { HttpURLConnection connection = null; InputStream is = null; BufferedReader br = null; String result = null; //返回結果字串 try { //建立遠端url連線物件 URL url = new
URL(httpurl); //通過遠端url連線物件開啟一個連線,強轉成httpURLConnection類 connection = (HttpURLConnection) url.openConnection(); //設定連線方式:get connection.setRequestMethod("GET"); //設定連線主機伺服器的超時時間:15000毫秒 connection.setConnectTimeout(15000); //設定讀取遠端返回的資料時間:60000毫秒
connection.setReadTimeout(60000); //傳送請求 connection.connect(); //通過connection連線,獲取輸入流 if (connection.getResponseCode() == 200) { is = connection.getInputStream(); //封裝輸入流is,並指定字符集 br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); //存放資料 StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\r\n"); } result = sb.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { //關閉資源 if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } assert connection != null; connection.disconnect(); //關閉遠端連線 } return result; } //POST方法 public static String doPost(String httpUrl, String param) { HttpURLConnection connection = null; InputStream is = null; OutputStream os = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpUrl); //通過遠端url連線物件開啟連線 connection = (HttpURLConnection) url.openConnection(); //設定連線請求方式 connection.setRequestMethod("POST"); //設定連線主機伺服器超時時間:15000毫秒 connection.setConnectTimeout(15000); //設定讀取主機伺服器返回資料超時時間:60000毫秒 connection.setReadTimeout(60000); //預設值為:false,當向遠端伺服器傳送資料/寫資料時,需要設定為true connection.setDoOutput(true); //預設值為:true,當前向遠端服務讀取資料時,設定為true,該引數可有可無 connection.setDoInput(true); //設定傳入引數的格式:請求引數應該是 name1=value1&name2=value2 的形式。 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //設定鑑權資訊:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); //通過連線物件獲取一個輸出流 os = connection.getOutputStream(); //通過輸出流物件將引數寫出去/傳輸出去,它是通過位元組陣列寫出的 os.write(param.getBytes()); //通過連線物件獲取一個輸入流,向遠端讀取 if (connection.getResponseCode() == 200) { is = connection.getInputStream(); //對輸入流物件進行包裝:charset根據工作專案組的要求來設定 br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); String line; //迴圈遍歷一行一行讀取資料 while ((line = br.readLine()) != null) { sb.append(line); sb.append("\r\n"); } result = sb.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { //關閉資源 if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } //斷開與遠端地址url的連線 assert connection != null; connection.disconnect(); } return result; } }