HttpInvoker http請求工具類

來源:互聯網
上載者:User

標籤:dir   tostring   false   invoke   throws   lin   bst   buffer   遠端連線   

 

 1 import java.io.BufferedReader; 2 import java.io.DataOutputStream; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 import java.util.Map; 8  9 /**10  * HTTP請求類11  * @author 12  */13 public class HttpInvoker {14 15     /**16      * GET請求17      * @param getUrl18      * @throws IOException19      * @return 提取HTTP響應報文包體,以字串形式返回20      */21     public static String httpGet(String getUrl) throws IOException { 22         //1 把字串形式的網址轉成 java.net.URL對象23         URL getURL = new URL(getUrl); 24         /*25          * 2 通過java.net.URL開啟一個串連(擷取一個代表與遠程對象的URLConnection對象),並轉為 HttpURLConnection ,26             Returns a URLConnection object that represents a connection 27             to the remote object referred to by the URL.28          */29         HttpURLConnection connection = (HttpURLConnection) getURL.openConnection(); 30         //3 通過HttpURLConnection對象真正擷取串連31         connection.connect();32         //4 串連後那麼這個遠端連線對象就可以通過自身的一些方法讀取到串連的資訊了,這裡是把流轉成字串33         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));34         StringBuilder sbStr = new StringBuilder();35         String line;36         while ((line = bufferedReader.readLine()) != null) { 37             sbStr.append(line); 38         } 39         bufferedReader.close();40         connection.disconnect(); 41         return new String(sbStr.toString().getBytes(),"utf-8");42     }43     44     45     public static void main(String[] args) throws IOException {46         String url = "https://www.hao123.com/?tn=99682755_hao_pg";47         String rtn_json = httpGet(url);48         System.out.println(""+rtn_json);49     }50     51     /**52      * POST請求53      * @param postUrl54      * @param postHeaders55      * @param postEntity56      * @throws IOException57      * @return 提取HTTP響應報文包體,以字串形式返回58      */59     public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {60         61         URL postURL = new URL(postUrl); 62         HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection(); 63         httpURLConnection.setDoOutput(true);                 64         httpURLConnection.setDoInput(true); 65         httpURLConnection.setRequestMethod("POST"); 66         httpURLConnection.setUseCaches(false); 67         httpURLConnection.setInstanceFollowRedirects(true); 68         httpURLConnection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded ");69 70         if(postHeaders != null) {71             for(String pKey : postHeaders.keySet()) {72                 httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));73             }74         }75         if(postEntity != null) {76             DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream()); 77             out.writeBytes(postEntity); 78             out.flush(); 79             out.close(); // flush and close 80         }81         //connection.connect(); 82         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 83         StringBuilder sbStr = new StringBuilder();84         String line;85         while ((line = bufferedReader.readLine()) != null) { 86             sbStr.append(line); 87         } 88         bufferedReader.close();89         httpURLConnection.disconnect(); 90         return new String(sbStr.toString().getBytes(),"utf-8");91     } 92 93 }

 

HttpInvoker http請求工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.