標籤:msu stream field tac 緩衝 請求 name 使用 user
1 package com.teamsun.pay.wxpay.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 public class HttpRequest { 13 /** 14 * 向指定URL發送GET方法的請求 15 * 16 * @param url 17 * 發送請求的URL 18 * @param param 19 * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 20 * @return URL 所代表遠端資源的響應結果 21 */ 22 public static String sendGet(String url, String param) { 23 String result = ""; 24 BufferedReader in = null; 25 try { 26 String urlNameString = url + "?" + param; 27 URL realUrl = new URL(urlNameString); 28 // 開啟和URL之間的串連 29 URLConnection connection = realUrl.openConnection(); 30 // 設定通用的請求屬性 31 connection.setRequestProperty("accept", "*/*"); 32 connection.setRequestProperty("connection", "Keep-Alive"); 33 connection.setRequestProperty("user-agent", 34 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 35 //setConnectTimeout:設定串連主機逾時(單位:毫秒) 36 connection.setConnectTimeout(2000);//2秒逾時 37 //setReadTimeout:設定從主機讀取資料逾時(單位:毫秒) 38 //connection.setReadTimeout(2000); 39 40 // 建立實際的串連 41 connection.connect(); 42 // 擷取所有回應標頭欄位 43 Map<String, List<String>> map = connection.getHeaderFields(); 44 // 遍曆所有的回應標頭欄位 45 for (String key : map.keySet()) { 46 System.out.println(key + "--->" + map.get(key)); 47 } 48 // 定義 BufferedReader輸入資料流來讀取URL的響應 49 in = new BufferedReader(new InputStreamReader( 50 connection.getInputStream())); 51 String line; 52 while ((line = in.readLine()) != null) { 53 result += line; 54 } 55 } catch (Exception e) { 56 System.out.println("發送GET請求出現異常!" + e); 57 e.printStackTrace(); 58 } 59 // 使用finally塊來關閉輸入資料流 60 finally { 61 try { 62 if (in != null) { 63 in.close(); 64 } 65 } catch (Exception e2) { 66 e2.printStackTrace(); 67 } 68 } 69 return result; 70 } 71 72 /** 73 * 向指定 URL 發送POST方法的請求 74 * 75 * @param url 76 * 發送請求的 URL 77 * @param param 78 * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 79 * @return 所代表遠端資源的響應結果 80 */ 81 public static String sendPost(String url, String param) { 82 PrintWriter out = null; 83 BufferedReader in = null; 84 String result = ""; 85 try { 86 URL realUrl = new URL(url); 87 // 開啟和URL之間的串連 88 URLConnection conn = realUrl.openConnection(); 89 // 設定通用的請求屬性 90 conn.setRequestProperty("accept", "*/*"); 91 conn.setRequestProperty("connection", "Keep-Alive"); 92 conn.setRequestProperty("user-agent", 93 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 94 // 發送POST請求必須設定如下兩行 95 conn.setDoOutput(true); 96 conn.setDoInput(true); 97 // 擷取URLConnection對象對應的輸出資料流 98 out = new PrintWriter(conn.getOutputStream()); 99 // 發送請求參數100 out.print(param);101 // flush輸出資料流的緩衝102 out.flush();103 // 定義BufferedReader輸入資料流來讀取URL的響應104 in = new BufferedReader(105 new InputStreamReader(conn.getInputStream()));106 String line;107 while ((line = in.readLine()) != null) {108 result += line;109 }110 } catch (Exception e) {111 System.out.println("發送 POST 請求出現異常!"+e);112 e.printStackTrace();113 }114 //使用finally塊來關閉輸出資料流、輸入資料流115 finally{116 try{117 if(out!=null){118 out.close();119 }120 if(in!=null){121 in.close();122 }123 }124 catch(IOException ex){125 ex.printStackTrace();126 }127 }128 return result;129 }130 131 132 public static void main(String[] args) {133 134 135 }136 }137 138
java中原生的發送http請求(無任何的jar包匯入)