2d3b128963ba474e43b48736ff4683d94aa2543e.svn-base 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package cn.com.goldenwater.dcproj.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import org.springframework.util.StringUtils;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.Iterator;
  13. import java.util.Map;
  14. public class HttpHeadHelper {
  15. public static String doPost(String httpUrl, Map<String, Object> mapParam, Map<String,String> head) throws IOException {
  16. return doPost(httpUrl, mapParam, null, null, 0, 0, head);
  17. }
  18. /***
  19. * 发起POST方法请求
  20. * @param httpUrl
  21. * @param mapParam
  22. * @param contentType
  23. * @param encoding
  24. * @param connectTimeOut
  25. * @param readTimeout
  26. * @param head
  27. * @return
  28. * @throws IOException
  29. */
  30. public static String doPost(String httpUrl, Map<String, Object> mapParam, String contentType, String encoding, int connectTimeOut, int readTimeout, Map<String,String> head) throws IOException {
  31. if (StringUtils.isEmpty(encoding)) {
  32. encoding = "UTF-8";
  33. }
  34. if (StringUtils.isEmpty(contentType)) {
  35. contentType = "application/json";
  36. }
  37. if (connectTimeOut <= 0) {
  38. connectTimeOut = 3000;
  39. }
  40. if (readTimeout <= 0) {
  41. readTimeout = 60000;
  42. }
  43. HttpURLConnection connection = null;
  44. InputStream is = null;
  45. OutputStream os = null;
  46. BufferedReader br = null;
  47. String result = null;
  48. try {
  49. URL url = new URL(httpUrl);
  50. connection = (HttpURLConnection)url.openConnection();
  51. connection.setRequestMethod("POST");
  52. connection.setConnectTimeout(connectTimeOut);
  53. connection.setReadTimeout(readTimeout);
  54. connection.setDoOutput(true);
  55. connection.setDoInput(true);
  56. connection.setRequestProperty("Content-Type", contentType);
  57. if(head!=null && !head.isEmpty()){
  58. for(Map.Entry<String,String> entry: head.entrySet()){
  59. connection.addRequestProperty(entry.getKey(),entry.getValue());
  60. }
  61. }
  62. os = connection.getOutputStream();
  63. // 根据contentType不同添加参数
  64. String param = null;
  65. Map.Entry item;
  66. if (!"application/json".equals(contentType)) {
  67. if (null != mapParam && mapParam.size() > 0) {
  68. param = "";
  69. for(Iterator var14 = mapParam.entrySet().iterator(); var14.hasNext(); param = param + (String)item.getKey() + "=" + item.getValue()) {
  70. item = (Map.Entry)var14.next();
  71. if (param.length() > 0) {
  72. param = param + "&";
  73. }
  74. }
  75. }
  76. } else if (null != mapParam && mapParam.keySet().size() > 0) {
  77. param = JSON.toJSONString(mapParam);
  78. }
  79. if (null != param && !"".equals(param.trim())) {
  80. os.write(param.getBytes("UTF-8"));
  81. }
  82. if (connection.getResponseCode() == 200) {
  83. is = connection.getInputStream();
  84. br = new BufferedReader(new InputStreamReader(is, encoding));
  85. StringBuffer sbf = new StringBuffer();
  86. String temp = null;
  87. while((temp = br.readLine()) != null) {
  88. sbf.append(temp);
  89. sbf.append("\r\n");
  90. }
  91. result = sbf.toString();
  92. }
  93. } catch (MalformedURLException var28) {
  94. throw var28;
  95. } catch (IOException var29) {
  96. throw var29;
  97. } finally {
  98. if (null != br) {
  99. try {
  100. br.close();
  101. } catch (IOException var27) {
  102. var27.printStackTrace();
  103. }
  104. }
  105. if (null != os) {
  106. try {
  107. os.close();
  108. } catch (IOException var26) {
  109. var26.printStackTrace();
  110. }
  111. }
  112. if (null != is) {
  113. try {
  114. is.close();
  115. } catch (IOException var25) {
  116. var25.printStackTrace();
  117. }
  118. }
  119. connection.disconnect();
  120. }
  121. return result;
  122. }
  123. public static String doGet(String url, String encoding, int connectTimeOut, int readTimeout, Map<String,String> head) throws IOException {
  124. if (connectTimeOut <= 0) {
  125. connectTimeOut = 3000;
  126. }
  127. if (readTimeout <= 0) {
  128. readTimeout = 60000;
  129. }
  130. if (null == encoding || encoding.isEmpty()) {
  131. encoding = "UTF-8";
  132. }
  133. InputStream in = null;
  134. BufferedReader br = null;
  135. String result = null;
  136. try {
  137. URL realUrl = new URL(url);
  138. HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
  139. connection.setRequestProperty("accept", "*/*");
  140. connection.setRequestProperty("connection", "Keep-Alive");
  141. 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");
  142. connection.setRequestMethod("GET");
  143. connection.setConnectTimeout(connectTimeOut);
  144. connection.setReadTimeout(readTimeout);
  145. if(head!=null && !head.isEmpty()){
  146. for(Map.Entry<String,String> entry: head.entrySet()){
  147. connection.addRequestProperty(entry.getKey(),entry.getValue());
  148. }
  149. }
  150. connection.connect();
  151. if (connection.getResponseCode() == 200) {
  152. in = connection.getInputStream();
  153. br = new BufferedReader(new InputStreamReader(in, encoding));
  154. StringBuffer sbf = new StringBuffer();
  155. String temp = null;
  156. while((temp = br.readLine()) != null) {
  157. sbf.append(temp);
  158. sbf.append("\r\n");
  159. }
  160. result = sbf.toString();
  161. }
  162. } catch (MalformedURLException var22) {
  163. throw var22;
  164. } catch (IOException var23) {
  165. throw var23;
  166. } finally {
  167. if (null != br) {
  168. try {
  169. br.close();
  170. } catch (IOException var21) {
  171. }
  172. }
  173. if (null != in) {
  174. try {
  175. in.close();
  176. } catch (IOException var20) {
  177. }
  178. }
  179. }
  180. return result;
  181. }
  182. public static String doGet(String httpUrl, String contentType, String encoding, int connectTimeOut, int readTimeout, Map<String,String> head) throws IOException {
  183. if (StringUtils.isEmpty(encoding)) {
  184. encoding = "UTF-8";
  185. }
  186. if (StringUtils.isEmpty(contentType)) {
  187. contentType = "application/json";
  188. }
  189. if (connectTimeOut <= 0) {
  190. connectTimeOut = 3000;
  191. }
  192. if (readTimeout <= 0) {
  193. readTimeout = 60000;
  194. }
  195. HttpURLConnection connection = null;
  196. InputStream is = null;
  197. BufferedReader br = null;
  198. String result = null;
  199. try {
  200. URL url = new URL(httpUrl);
  201. connection = (HttpURLConnection)url.openConnection();
  202. connection.setRequestMethod("GET");
  203. connection.setConnectTimeout(connectTimeOut);
  204. connection.setReadTimeout(readTimeout);
  205. connection.setDoOutput(true);
  206. connection.setDoInput(true);
  207. connection.setRequestProperty("Content-Type", contentType);
  208. if(head!=null && !head.isEmpty()){
  209. for(Map.Entry<String,String> entry: head.entrySet()){
  210. connection.addRequestProperty(entry.getKey(),entry.getValue());
  211. }
  212. }
  213. if (connection.getResponseCode() == 200) {
  214. is = connection.getInputStream();
  215. br = new BufferedReader(new InputStreamReader(is, encoding));
  216. StringBuffer sbf = new StringBuffer();
  217. String temp = null;
  218. while((temp = br.readLine()) != null) {
  219. sbf.append(temp);
  220. sbf.append("\r\n");
  221. }
  222. result = sbf.toString();
  223. }
  224. } catch (MalformedURLException var28) {
  225. throw var28;
  226. } catch (IOException var29) {
  227. throw var29;
  228. } finally {
  229. if (null != br) {
  230. try {
  231. br.close();
  232. } catch (IOException var27) {
  233. var27.printStackTrace();
  234. }
  235. }
  236. if (null != is) {
  237. try {
  238. is.close();
  239. } catch (IOException var25) {
  240. var25.printStackTrace();
  241. }
  242. }
  243. connection.disconnect();
  244. }
  245. return result;
  246. }
  247. }