HttpClient 呼叫其它系統介面的工具類(post、get請求)
阿新 • • 發佈:2019-02-10
功能介紹:
1)實現了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
2)支援自動轉向
3)支援 HTTPS 協議
4)支援代理伺服器等
HttpClient最重要的功能是執行HTTP方法:執行一個HTTP方法涉及一個或多個HTTP請求/ HTTP響應資訊交流,通常是由HttpClient內部處理。使用者提供一個請求物件,HttpClient傳送請求到目標伺服器,希望伺服器返回一個相應的響應物件,或者丟擲一個異常(如果執行失敗)。
示例工具類:
package com.forezp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Map; public class HttpClientUtils { /** * 向指定URL傳送GET方法的請求 * * @param url 傳送請求的URL 例如:http://localhost:8080/demo/login * @param param 請求引數 例:{ "userName":"admin", "password":"123456" } * @return URL 所代表遠端資源的響應結果 */ public static String sendGet(String url, Map<String, Object> param) { StringBuilder result = new StringBuilder(); BufferedReader in = null; try { URL realUrl = createURL(url, param); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); conn.connect(); in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { System.out.println("傳送GET請求出現異常!" + e); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result.toString(); } /** * 向指定 URL 傳送POST方法的請求 * * @param url 傳送請求的URL 例如:http://localhost:8080/demo/login * @param param 請求引數 例:{ "userName":"admin", "password":"123456" } * @return 所代表遠端資源的響應結果 */ public static String sendPost(String url, Map<String, Object> param) { BufferedReader in = null; StringBuilder result = new StringBuilder(); try { URL realUrl = createURL(url, param); // 開啟和URL之間的連線 HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); // 傳送POST請求必須設定 conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); // 設定通用的請求屬性 conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.connect(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { System.out.println("傳送 POST 請求出現異常!" + e); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result.toString(); } private static URL createURL(String url, Map<String, Object> param) throws MalformedURLException { if (param == null || param.size() == 0){ return new URL(url); } StringBuilder sb = new StringBuilder(url); sb.append("?"); for (String key : param.keySet()) { sb.append(key); sb.append("="); sb.append(encode(String.valueOf( param.get(key)))); sb.append("&"); } return new URL(sb.substring(0, sb.length() - 1)); } private static String encode(String text) { try { return URLEncoder.encode(text, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } }