Java URLConnection請求的引數進行封裝
阿新 • • 發佈:2019-01-08
package com.hospital.netengine; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; /** * 網路請求入口 * * @author scott * */ public class NetHttpClient { /** * 向指定URL傳送GET方法的請求 * * @param url * 傳送請求的URL * @param param * 請求引數 * @return 請求結果 */ public static String sendGet(String url, Map<String, String> params) { StringBuilder result = new StringBuilder(); BufferedReader in = null; try { String urlNameString = url + "?" + getUrlParamsFromMap(params); URL realUrl = new URL(urlNameString); // 開啟和URL之間的連線 URLConnection connection = realUrl.openConnection(); // 設定通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立實際的連線 connection.connect(); // 獲取所有響應頭欄位 Map<String, List<String>> map = connection.getHeaderFields(); System.out.println("響應頭資訊:"); // 遍歷所有的響應頭欄位 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); }finally {// 使用finally塊來關閉輸入流 try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result.toString(); } /** * 傳送POST方法的請求 * * @param url * 傳送請求的 URL * @param param * 請求引數。 * @return 響應結果 */ public static String sendPost(String url, Map<String, String> params) { PrintWriter out = null; BufferedReader in = null; StringBuilder result = new StringBuilder(); try { URL realUrl = new URL(url); // 開啟和URL之間的連線 URLConnection conn = realUrl.openConnection(); // 設定通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 傳送POST請求必須設定如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection物件對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 傳送請求引數 out.print(getUrlParamsFromMap(params)); // flush輸出流的緩衝 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); }finally {// 使用finally塊來關閉輸出流、輸入流 try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result.toString(); } /** * description:將map轉換成url引數格式: name1=value1&name2=value2 * * @param map * @return */ public static String getUrlParamsFromMap(Map<String, String> map) { try { if (null != map) { StringBuilder stringBuilder = new StringBuilder(); for (Map.Entry<String, String> entry : map.entrySet()) { stringBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8")).append("=") .append(URLEncoder.encode(entry.getValue(), "UTF-8")).append("&"); } String content = stringBuilder.toString(); if (content.endsWith("&")) { content = StringUtils.substringBeforeLast(content, "&"); } return content; } } catch (Exception e) { e.printStackTrace(); } return null; } }