| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- 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<String, Object> mapParam, Map<String,String> 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<String, Object> mapParam, String contentType, String encoding, int connectTimeOut, int readTimeout, Map<String,String> 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<String,String> 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<String,String> 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<String,String> 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<String,String> 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<String,String> 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;
- }
- }
|