1 package com.ddtech.app.util; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.URL; 8 import java.net.URLConnection; 9 import java.util.List; 10 import java.util.Map; 11 12 import com.alibaba.fastjson.JSONObject; 13 14 public class HttpRequestGetAndPost { 15 /** 16 * 向指定URL發送GET方法的請求 17 * 18 * @param url 19 * 發送請求的URL 20 * @param param 21 * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 22 * @return URL 所表明遠程資源的響應結果 23 */ 24 public static String sendGet(String url, String param) { 25 String result = ""; 26 BufferedReader in = null; 27 try { 28 String urlNameString = url + "?" + param; 29 URL realUrl = new URL(urlNameString); 30 // 打開和URL之間的鏈接 31 URLConnection connection = realUrl.openConnection(); 32 // 設置通用的請求屬性 33 connection.setRequestProperty("accept", "*/*"); 34 connection.setRequestProperty("connection", "Keep-Alive"); 35 connection.setRequestProperty("user-agent", 36 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 37 // 創建實際的鏈接 38 connection.connect(); 39 // 獲取全部響應頭字段 40 Map<String, List<String>> map = connection.getHeaderFields(); 41 // 遍歷全部的響應頭字段 42 for (String key : map.keySet()) { 43 System.out.println(key + "--->" + map.get(key)); 44 } 45 // 定義 BufferedReader輸入流來讀取URL的響應 46 in = new BufferedReader(new InputStreamReader( 47 connection.getInputStream())); 48 String line; 49 while ((line = in.readLine()) != null) { 50 result += line; 51 } 52 } catch (Exception e) { 53 System.out.println("發送GET請求出現異常!" + e); 54 e.printStackTrace(); 55 } 56 // 使用finally塊來關閉輸入流 57 finally { 58 try { 59 if (in != null) { 60 in.close(); 61 } 62 } catch (Exception e2) { 63 e2.printStackTrace(); 64 } 65 } 66 return result; 67 } 68 69 /** 70 * 向指定 URL 發送POST方法的請求 71 * 72 * @param url 73 * 發送請求的 URL 74 * @param param 75 * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 76 * @return 所表明遠程資源的響應結果 77 */ 78 public static String sendPost(String url, JSONObject param) { 79 PrintWriter out = null; 80 BufferedReader in = null; 81 String result = ""; 82 try { 83 URL realUrl = new URL(url); 84 // 打開和URL之間的鏈接 85 URLConnection conn = realUrl.openConnection(); 86 // 設置通用的請求屬性 87 conn.setRequestProperty("accept", "*/*"); 88 conn.setRequestProperty("connection", "Keep-Alive"); 89 conn.setRequestProperty("user-agent", 90 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 91 // 發送POST請求必須設置以下兩行 92 conn.setDoOutput(true); 93 conn.setDoInput(true); 94 // 獲取URLConnection對象對應的輸出流 95 out = new PrintWriter(conn.getOutputStream()); 96 // 發送請求參數 97 out.print(param); 98 // flush輸出流的緩衝 99 out.flush(); 100 // 定義BufferedReader輸入流來讀取URL的響應 101 in = new BufferedReader( 102 new InputStreamReader(conn.getInputStream())); 103 String line; 104 while ((line = in.readLine()) != null) { 105 result += line; 106 } 107 } catch (Exception e) { 108 System.out.println("發送 POST 請求出現異常!" + e); 109 e.printStackTrace(); 110 } 111 // 使用finally塊來關閉輸出流、輸入流 112 finally { 113 try { 114 if (out != null) { 115 out.close(); 116 } 117 if (in != null) { 118 in.close(); 119 } 120 } catch (IOException ex) { 121 ex.printStackTrace(); 122 } 123 } 124 return result; 125 } 126 }