package zr.weixin.com.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import java.util.Map.Entry; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class HttpClientHelper { /** * http请求(GET) */ public static String sendGet(String urlParam, Map<String, Object> params, String charset) { StringBuffer resultBuffer = null; // 拼接字段 StringBuffer sbParams = new StringBuffer(); if (params != null && params.size() > 0) { for (Entry<String, Object> entry : params.entrySet()) { sbParams.append(entry.getKey()); sbParams.append("="); sbParams.append(entry.getValue()); sbParams.append("&"); } } HttpURLConnection con = null; BufferedReader br = null; // url与拼接好的字段连接在一起 try { URL url = null; if (sbParams != null && sbParams.length() > 0) { url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1)); } else { url = new URL(urlParam); } // 打开连接 con = (HttpURLConnection) url.openConnection(); // 设置请求头 con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 开启连接 con.connect(); // 将返回的数据转成StringBuffer对象 resultBuffer = new StringBuffer(); br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset)); String temp; while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (br != null) { try { br.close(); } catch (IOException e) { br = null; throw new RuntimeException(e); } finally { if (con != null) { con.disconnect(); con = null; } } } } return resultBuffer.toString(); } /** * http请求(POST) * @param urlParam * @param params * @param charset * @return */ public static String sendPost(String urlParam,Map<String, Object> params,String charset){ StringBuffer resultBuffer = null; // 拼接字段 StringBuffer sbParams = new StringBuffer(); if (params != null && params.size() > 0) { for (Entry<String, Object> e : params.entrySet()) { sbParams.append(e.getKey()); sbParams.append("="); sbParams.append(e.getValue()); sbParams.append("&"); } } HttpURLConnection con = null; OutputStreamWriter osw = null; BufferedReader br = null; // 设置请求相应的参数 try { URL url = new URL(urlParam); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); if (sbParams != null && sbParams.length() > 0) { osw = new OutputStreamWriter(con.getOutputStream(), charset); osw.write(sbParams.substring(0, sbParams.length() - 1)); osw.flush(); } // 将返回的数据转成StringBuffer对象 resultBuffer = new StringBuffer(); int contentLength = Integer.parseInt(con.getHeaderField("Content-Length")); if (contentLength > 0) { br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset)); String temp; while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } } } catch (Exception e) { throw new RuntimeException(e); } finally { if (osw != null) { try { osw.close(); } catch (IOException e) { osw = null; throw new RuntimeException(e); } finally { if (con != null) { con.disconnect(); con = null; } } } if (br != null) { try { br.close(); } catch (IOException e) { br = null; throw new RuntimeException(e); } finally { if (con != null) { con.disconnect(); con = null; } } } } return resultBuffer.toString(); } /** * post请求 * @param urlParam * @param Json * @param charset * @return */ public static String sendPost(String urlParam,String Json, String charset) { StringBuffer resultBuffer = null; HttpURLConnection con = null; OutputStreamWriter osw = null; BufferedReader br = null; // 发送请求 try { URL url = new URL(urlParam); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); if (Json!=null&&Json.length()>0) { osw = new OutputStreamWriter(con.getOutputStream(), charset); osw.write(Json); osw.flush(); } // 读取返回内容 resultBuffer = new StringBuffer(); int contentLength = Integer.parseInt(con.getHeaderField("Content-Length")); if (contentLength > 0) { br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset)); String temp; while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } } } catch (Exception e) { throw new RuntimeException(e); } finally { if (osw != null) { try { osw.close(); } catch (IOException e) { osw = null; throw new RuntimeException(e); } finally { if (con != null) { con.disconnect(); con = null; } } } if (br != null) { try { br.close(); } catch (IOException e) { br = null; throw new RuntimeException(e); } finally { if (con != null) { con.disconnect(); con = null; } } } } return resultBuffer.toString(); } }