HttpClient中GET和POST請求方式詳解

2021-10-04 17:04:27 字數 1181 閱讀 3201

前述,個人小結

使用httpclient傳送請求、接收響應很簡單,一般需要如下幾步即可。

1. 建立httpclient物件。

2. 建立請求方法的例項,並指定請求url。如果需要傳送get請求,建立httpget物件;如果需要傳送post請求,建立httppost物件。

3. 如果需要傳送請求引數,可呼叫httpget、httppost共同的setparams(httpparams params)方法來新增請求引數;對於httppost物件而言,也可呼叫setentity(httpentity entity)方法來設定請求引數。

4. 呼叫httpclient物件的execute(httpurirequest request)傳送請求,該方法返回乙個httpresponse。

5. 呼叫httpresponse的getallheaders()、getheaders(string name)等方法可獲取伺服器的響應頭;呼叫httpresponse的getentity()方法可獲取httpentity物件,該物件包裝了伺服器的響應內容。程式可通過該物件獲取伺服器的響應內容。

6. 釋放連線。無論執行方法是否成功,都必須釋放連線

第一、匯入httpclient的pom依賴

注意:版本號可選

二、**實現---httpclient傳送get請求(帶引數直接在ulr後跟引數拼接)

// 關閉

response.close();

}httpclient傳送post請求(帶引數請求可看參考如下**)

// 關閉

response.close();

}

Post和Get在HttpClient的使用

httpclient提供的主要的功能如下 實現了所有http的方法 get post put head等 支援自動轉向 支援https協議 支援 伺服器 http請求方法中最常用的是get方法和post方法。1 get方法 get方法要求伺服器將url定位的資源放在響應報文的資料部分,回送給客戶端。...

HttpClient的Post和Get方法

1.不帶引數的get請求 不帶引數的get請求 public static void dogetwithoutparameter throws exception 2.帶引數的get請求 代引數的get請求 public static void dogetwithparameter throws e...

使用httpClient傳送請求get和post

匯入httpclient相關的jar包 2.使用httpclient傳送get請求 public static void main string args throws exception 6.3 獲取響應體 7 釋放資源 3 使用httpclient傳送post請求 public static v...