国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java發送http的get、post請求

2019-11-14 23:58:53
字體:
來源:轉載
供稿:網友
java發送http的get、post請求

直接上代碼

  1 package wzh.Http;  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             // 建立實際的連接 36             connection.connect(); 37             // 獲取所有響應頭字段 38             Map<String, List<String>> map = connection.getHeaderFields(); 39             // 遍歷所有的響應頭字段 40             for (String key : map.keySet()) { 41                 System.out.println(key + "--->" + map.get(key)); 42             } 43             // 定義 BufferedReader輸入流來讀取URL的響應 44             in = new BufferedReader(new InputStreamReader( 45                     connection.getInputStream())); 46             String line; 47             while ((line = in.readLine()) != null) { 48                 result += line; 49             } 50         } catch (Exception e) { 51             System.out.println("發送GET請求出現異常!" + e); 52             e.printStackTrace(); 53         } 54         // 使用finally塊來關閉輸入流 55         finally { 56             try { 57                 if (in != null) { 58                     in.close(); 59                 } 60             } catch (Exception e2) { 61                 e2.printStackTrace(); 62             } 63         } 64         return result; 65     } 66  67     /** 68      * 向指定 URL 發送POST方法的請求 69      *  70      * @param url 71      *            發送請求的 URL 72      * @param param 73      *            請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 74      * @return 所代表遠程資源的響應結果 75      */ 76     public static String sendPost(String url, String param) { 77         PrintWriter out = null; 78         BufferedReader in = null; 79         String result = ""; 80         try { 81             URL realUrl = new URL(url); 82             // 打開和URL之間的連接 83             URLConnection conn = realUrl.openConnection(); 84             // 設置通用的請求屬性 85             conn.setRequestProperty("accept", "*/*"); 86             conn.setRequestProperty("connection", "Keep-Alive"); 87             conn.setRequestProperty("user-agent", 88                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 89             // 發送POST請求必須設置如下兩行 90             conn.setDoOutput(true); 91             conn.setDoInput(true); 92             // 獲取URLConnection對象對應的輸出流 93             out = new PrintWriter(conn.getOutputStream()); 94             // 發送請求參數 95             out.print(param); 96             // flush輸出流的緩沖 97             out.flush(); 98             // 定義BufferedReader輸入流來讀取URL的響應 99             in = new BufferedReader(100                     new InputStreamReader(conn.getInputStream()));101             String line;102             while ((line = in.readLine()) != null) {103                 result += line;104             }105         } catch (Exception e) {106             System.out.println("發送 POST 請求出現異常!"+e);107             e.printStackTrace();108         }109         //使用finally塊來關閉輸出流、輸入流110         finally{111             try{112                 if(out!=null){113                     out.close();114                 }115                 if(in!=null){116                     in.close();117                 }118             }119             catch(IOException ex){120                 ex.printStackTrace();121             }122         }123         return result;124     }    125 }

調用

 1 public static void main(String[] args) { 2      3         //發送 GET 請求 4         String s=HttpRequest.sendGet("url", "params"); 5         System.out.println(s); 6          7         //發送 POST 請求 8         String sr=HttpRequest.sendPost("url", "params"); 9         System.out.println(sr);10     }//url  自己的鏈接地址 params  傳遞的參數 & 相連


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镶黄旗| 榆社县| 崇左市| 慈溪市| 侯马市| 曲阳县| 龙海市| 台中县| 平遥县| 思茅市| 洪湖市| 灌云县| 广宗县| 乐至县| 岳阳县| 正安县| 年辖:市辖区| 紫阳县| 娱乐| 乌兰察布市| 夏邑县| 丘北县| 北川| 锡林郭勒盟| 炎陵县| 横山县| 新乡市| 衡阳市| 哈尔滨市| 涟源市| 阳原县| 商丘市| 遵义市| 衢州市| 西盟| 清原| 西盟| 宁明县| 衢州市| 镇坪县| 原阳县|