什么是HTTP?
超文本傳輸協議(HyperText Transfer Protocol -- HTTP)是一個設計來使客戶端和服務器順利進行通訊的協議。
HTTP在客戶端和服務器之間以request-response protocol(請求-回復協議)工作。
GET和POST是HTTP的兩個常用方法。
GET - 從指定的服務器中獲取數據
POST - 提交數據給指定的服務器處理
GET方法:
使用GET方法時,查詢字符串(鍵值對)被附加在URL地址后面一起發送到服務器:
/test/demo_form.jsp?name1=value1&name2=value2
特點:
- GET請求能夠被緩存
- GET請求會保存在瀏覽器的瀏覽記錄中
- 以GET請求的URL能夠保存為瀏覽器書簽
- GET請求有長度限制
- GET請求主要用以獲取數據
POST方法:
使用POST方法時,查詢字符串在POST信息中單獨存在,和HTTP請求一起發送到服務器:
POST /test/demo_form.jsp HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
特點:
- POST請求不能被緩存下來
- POST請求不會保存在瀏覽器瀏覽記錄中
- 以POST請求的URL無法保存為瀏覽器書簽
- POST請求沒有長度限制
GET和POST的區別:
GET | POST | |
點擊返回/刷新按鈕 | 沒有影響 | 數據會重新發送(瀏覽器將會提示用戶“數據被從新提交”) |
添加書簽 | 可以 | 不可以 |
緩存 | 可以 | 不可以 |
編碼類型(Encoding type) | application/x-www-form-urlencoded |
application/x-www-form-urlencoded or multipart/form-data. 請為二進制數據使用multipart編碼
|
歷史記錄 | 有 | 沒有 |
長度限制 | 有 | 沒有 |
數據類型限制 | 只允許ASCII字符類型 | 沒有限制。允許二進制數據 |
安全性 | 查詢字符串會顯示在地址欄的URL中,不安全,請不要使用GET請求提交敏感數據 | 因為數據不會顯示在地址欄中,也不會緩存下來或保存在瀏覽記錄中,所以看POST求情比GET請求安全,但也不是最安全的方式。如需要傳送敏感數據,請使用加密方式傳輸 |
可見性 | 查詢字符串顯示在地址欄的URL中,可見 | 查詢字符串不會顯示在地址欄中,不可見 |
方式 | 描述 |
HEAD | 與GET請求類似,不同在與服務器只返回HTTP頭部信息,沒有頁面內容 |
PUT | 上傳指定URL的描述 |
DELETE | 刪除指定資源 |
OPTIONS | 返回服務器支持的HTTP方法 |
CONNECT | 轉換為透明TCP/IP隧道的連接請求 |
Java實現發送Get和Post請求的代碼如下
package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class HttpRequest { /** * 向指定URL發送GET方法的請求 * * @param url * 發送請求的URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠程資源的響應結果 */ public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立實際的連接 connection.connect(); // 獲取所有響應頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷所有的響應頭字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所代表遠程資源的響應結果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送 POST 請求出現異常!"+e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } }
調用方法

public static void main(String[] args) { //發送 GET 請求 String s=HttpRequest.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456"); System.out.println(s); //發送 POST 請求 String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456"); System.out.println(sr); }