網絡編程(發送get和post請求到服務器端,並獲取響應)


一:B/S結構,瀏覽器端到服務器端通信依賴http協議

交互過程:

1:在瀏覽器地址欄輸入http://ip:port/應用/資源路徑

2:瀏覽器根據ip和服務器建立連接,port確定和那個應用進行交互,因為ip主機上面有

很多應用程序。

3:瀏覽器端發送請求以及參數到服務器端,就是url(同一資源定位器),確定請求資源。

4:服務器處理請求,將處理后的結果響應發送到瀏覽器。

5:瀏覽器得到資源后,渲染到界面,顯示給用戶。

優點:B/S交互可以省去客戶端部署升級的風險,降低了成本,只需要關注服務器開發。

 

二:C/S結構,客戶端到服務器端通信也是依賴HTTP協議

主要是java應用程序與服務器的交互,交互過程:

1:根據請求url創建URL對象

2:創建連接對象URLConnection,設置連接參數,建立連接。

3:發送請求到服務器,服務器進行處理

4:服務器發送響應到客戶端。

 

三:get請求與post請求的異同點

1:get請求會將請求參數顯示在請求地址的后面,post不會,所以post相較於get更安全,但是安全是相對的,

如果追求安全發送請求還是要加密。

2:get請求發送數據理論上是4kb,而post理論上可以發送任意大的數據

3:如果是C/S結構,post請求在建立連接前,要設置DoInput和DoOutput的值

 

四:java程序請求服務器實例

get請求案例:

 1 /**
 2  * 發送get請求,參數放在url后面  3      */
 4     public static String sendGetRequest(String url, String params) {  5         StringBuilder result = new StringBuilder();  6         String realUrl = url + "?" + params;  7         InputStream in = null;  8         BufferedReader br = null;  9         try { 10             // 與服務器建立連接
11             URL u = new URL(realUrl); 12             URLConnection conn = u.openConnection(); 13             conn.setRequestProperty("accept", "*/*"); 14             conn.setRequestProperty("connection", "keep-alive"); 15  conn.connect(); 16 
17             // 獲取響應頭
18             Map<String, List<String>> map = conn.getHeaderFields(); 19             Set<String> set = map.keySet(); 20             Iterator<String> it = set.iterator(); 21             while (it.hasNext()) { 22                 String key = it.next(); 23                 System.out.println(key + ":::" + map.get(key)); 24  } 25 
26             // 獲取響應數據
27             in = conn.getInputStream(); 28             br = new BufferedReader(new InputStreamReader(in, "utf-8")); 29  String line; 30             while ((line = br.readLine()) != null) { 31  result.append(line); 32  } 33         } catch (Exception e) { 34  e.printStackTrace(); 35         } finally { 36             if (null != in) { 37                 try { 38  in.close(); 39                 } catch (IOException e) { 40  e.printStackTrace(); 41  } 42  } 43             if (null != br) { 44                 try { 45  br.close(); 46                 } catch (IOException e) { 47  e.printStackTrace(); 48  } 49  } 50  } 51         return result.toString(); 52     }

post請求案例:

 1     /**
 2  * 發送post請求,參數單獨發送到服務器端  3      */
 4     public static String sendPostRequest(String url, String params) {  5         StringBuilder result = new StringBuilder();  6         String realUrl = url;  7         InputStream in = null;  8         BufferedReader br = null;  9         try { 10             // 與服務器建立連接
11             URL u = new URL(realUrl); 12             URLConnection conn = u.openConnection(); 13             conn.setRequestProperty("accept", "*/*"); 14             conn.setRequestProperty("connection", "keep-alive"); 15 
16             // post請求必須設置請求頭
17             conn.setDoInput(true); 18             conn.setDoOutput(true); 19  conn.connect(); 20 
21             // 發送參數到服務器
22             OutputStream out = conn.getOutputStream(); 23             PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, 24                     "utf-8")); 25  pw.print(params); 26  pw.flush(); 27  pw.close(); 28 
29             // 獲取響應頭
30             Map<String, List<String>> map = conn.getHeaderFields(); 31             Set<Entry<String, List<String>>> entry = map.entrySet(); 32             Iterator<Entry<String, List<String>>> it = entry.iterator(); 33             while (it.hasNext()) { 34                 Entry<String, List<String>> en = it.next(); 35                 System.out.println(en.getKey() + ":::" + en.getValue()); 36  } 37 
38             // 獲取響應數據
39             in = conn.getInputStream(); 40             br = new BufferedReader(new InputStreamReader(in, "utf-8")); 41  String line; 42             while ((line = br.readLine()) != null) { 43  result.append(line); 44  } 45         } catch (Exception e) { 46  e.printStackTrace(); 47         } finally { 48             if (null != in) { 49                 try { 50  in.close(); 51                 } catch (IOException e) { 52  e.printStackTrace(); 53  } 54  } 55             if (null != br) { 56                 try { 57  br.close(); 58                 } catch (IOException e) { 59  e.printStackTrace(); 60  } 61  } 62  } 63         return result.toString(); 64     }

測試案例:

1 // 測試發送請求
2     public static void main(String[] args) { 3         String url = "http://localhost:8080/healthcare/dataAnalysis/search.do"; 4         String params = "tname=employee"; 5         // 測試get請求
6  System.out.println(SendGetAndPost.sendGetRequest(url, params)); 7         // 測試post請求
8  System.out.println(SendGetAndPost.sendPostRequest(url, params)); 9     }

以上代碼均已經過驗證。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM