package cn.com.goldenwater.dcproj.utils; import com.alibaba.fastjson.JSON; import org.springframework.util.StringUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.Map; public class HttpHeadHelper { public static String doPost(String httpUrl, Map mapParam, Map head) throws IOException { return doPost(httpUrl, mapParam, null, null, 0, 0, head); } /*** * 发起POST方法请求 * @param httpUrl * @param mapParam * @param contentType * @param encoding * @param connectTimeOut * @param readTimeout * @param head * @return * @throws IOException */ public static String doPost(String httpUrl, Map mapParam, String contentType, String encoding, int connectTimeOut, int readTimeout, Map head) throws IOException { if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } if (StringUtils.isEmpty(contentType)) { contentType = "application/json"; } if (connectTimeOut <= 0) { connectTimeOut = 3000; } if (readTimeout <= 0) { readTimeout = 60000; } HttpURLConnection connection = null; InputStream is = null; OutputStream os = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpUrl); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(connectTimeOut); connection.setReadTimeout(readTimeout); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", contentType); if(head!=null && !head.isEmpty()){ for(Map.Entry entry: head.entrySet()){ connection.addRequestProperty(entry.getKey(),entry.getValue()); } } os = connection.getOutputStream(); // 根据contentType不同添加参数 String param = null; Map.Entry item; if (!"application/json".equals(contentType)) { if (null != mapParam && mapParam.size() > 0) { param = ""; for(Iterator var14 = mapParam.entrySet().iterator(); var14.hasNext(); param = param + (String)item.getKey() + "=" + item.getValue()) { item = (Map.Entry)var14.next(); if (param.length() > 0) { param = param + "&"; } } } } else if (null != mapParam && mapParam.keySet().size() > 0) { param = JSON.toJSONString(mapParam); } if (null != param && !"".equals(param.trim())) { os.write(param.getBytes("UTF-8")); } if (connection.getResponseCode() == 200) { is = connection.getInputStream(); br = new BufferedReader(new InputStreamReader(is, encoding)); StringBuffer sbf = new StringBuffer(); String temp = null; while((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } } catch (MalformedURLException var28) { throw var28; } catch (IOException var29) { throw var29; } finally { if (null != br) { try { br.close(); } catch (IOException var27) { var27.printStackTrace(); } } if (null != os) { try { os.close(); } catch (IOException var26) { var26.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException var25) { var25.printStackTrace(); } } connection.disconnect(); } return result; } public static String doGet(String url, String encoding, int connectTimeOut, int readTimeout, Map head) throws IOException { if (connectTimeOut <= 0) { connectTimeOut = 3000; } if (readTimeout <= 0) { readTimeout = 60000; } if (null == encoding || encoding.isEmpty()) { encoding = "UTF-8"; } InputStream in = null; BufferedReader br = null; String result = null; try { URL realUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"); connection.setRequestMethod("GET"); connection.setConnectTimeout(connectTimeOut); connection.setReadTimeout(readTimeout); if(head!=null && !head.isEmpty()){ for(Map.Entry entry: head.entrySet()){ connection.addRequestProperty(entry.getKey(),entry.getValue()); } } connection.connect(); if (connection.getResponseCode() == 200) { in = connection.getInputStream(); br = new BufferedReader(new InputStreamReader(in, encoding)); StringBuffer sbf = new StringBuffer(); String temp = null; while((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } } catch (MalformedURLException var22) { throw var22; } catch (IOException var23) { throw var23; } finally { if (null != br) { try { br.close(); } catch (IOException var21) { } } if (null != in) { try { in.close(); } catch (IOException var20) { } } } return result; } public static String doGet(String httpUrl, String contentType, String encoding, int connectTimeOut, int readTimeout, Map head) throws IOException { if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } if (StringUtils.isEmpty(contentType)) { contentType = "application/json"; } if (connectTimeOut <= 0) { connectTimeOut = 3000; } if (readTimeout <= 0) { readTimeout = 60000; } HttpURLConnection connection = null; InputStream is = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpUrl); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(connectTimeOut); connection.setReadTimeout(readTimeout); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", contentType); if(head!=null && !head.isEmpty()){ for(Map.Entry entry: head.entrySet()){ connection.addRequestProperty(entry.getKey(),entry.getValue()); } } if (connection.getResponseCode() == 200) { is = connection.getInputStream(); br = new BufferedReader(new InputStreamReader(is, encoding)); StringBuffer sbf = new StringBuffer(); String temp = null; while((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } } catch (MalformedURLException var28) { throw var28; } catch (IOException var29) { throw var29; } finally { if (null != br) { try { br.close(); } catch (IOException var27) { var27.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException var25) { var25.printStackTrace(); } } connection.disconnect(); } return result; } }