bcc7a4b89d2de5c24a6e5fe98dbaa29176592395.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.chinacreator.videoalliance.order.util;
  2. import org.apache.commons.lang.StringUtils;
  3. import javax.net.ssl.KeyManager;
  4. import javax.net.ssl.SSLContext;
  5. import javax.net.ssl.TrustManager;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStreamWriter;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.security.KeyManagementException;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.security.SecureRandom;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. public class URLUtil {
  17. /**
  18. * 解读流
  19. * @param inStream
  20. * @return
  21. * @throws Exception
  22. */
  23. public static String readInputStream(InputStream inStream) throws Exception {
  24. ByteArrayOutputStream outStream = null;
  25. try {
  26. outStream = new ByteArrayOutputStream();
  27. byte[] buffer = new byte[1024];
  28. int len = 0;
  29. while ((len = inStream.read(buffer)) != -1) {
  30. outStream.write(buffer, 0, len);
  31. }
  32. byte[] data = outStream.toByteArray();
  33. return new String(data, "utf-8");
  34. } finally{
  35. if(outStream != null)
  36. outStream.close();
  37. if(inStream != null)
  38. inStream.close();
  39. }
  40. }
  41. /**
  42. * 以get方式获取链接的输出
  43. * @param href
  44. * @return
  45. * @throws Exception
  46. */
  47. public static String get(String href, int timeout) throws Exception {
  48. //改造
  49. try {
  50. SSLContext ctx = SSLContext.getInstance("TLSv1.2");
  51. ctx.init((KeyManager[])null, (TrustManager[])null, (SecureRandom)null);
  52. SSLContext.setDefault(ctx);
  53. } catch (NoSuchAlgorithmException var9) {
  54. var9.printStackTrace();
  55. } catch (KeyManagementException var10) {
  56. var10.printStackTrace();
  57. }
  58. //改造
  59. HttpURLConnection conn = null;
  60. try {
  61. URL url = new URL(href);
  62. conn = (HttpURLConnection) url.openConnection();
  63. conn.setRequestMethod("GET");
  64. if(timeout > 0) {
  65. conn.setConnectTimeout(timeout);
  66. conn.setReadTimeout(timeout);
  67. }
  68. conn.addRequestProperty("content-type", "text/plain");
  69. conn.setUseCaches(false);
  70. conn.setDoOutput(false);
  71. return readInputStream(conn.getInputStream());
  72. } finally {
  73. if(conn != null) {
  74. conn.disconnect();
  75. }
  76. }
  77. }
  78. public static String get(String href) throws Exception {
  79. return get(href, 0);
  80. }
  81. /**
  82. * 以post方式获取链接的输出
  83. * @param href
  84. * @return
  85. * @throws Exception
  86. */
  87. public static String post(String href, String data, int timeout, String contentType) throws Exception {
  88. HttpURLConnection conn = null;
  89. OutputStreamWriter out = null;
  90. if(data == null || data.length() == 0) {
  91. return get(href, timeout);
  92. } else {
  93. try {
  94. URL url = new URL(href);
  95. conn = (HttpURLConnection) url.openConnection();
  96. conn.setRequestMethod("POST");
  97. if(timeout > 0) {
  98. conn.setConnectTimeout(timeout);
  99. conn.setReadTimeout(timeout);
  100. }
  101. conn.addRequestProperty("content-type", contentType);
  102. conn.setDoOutput(true);
  103. conn.setUseCaches(false);
  104. out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  105. out.write(data);
  106. out.flush();
  107. } finally {
  108. if(out != null) {
  109. out.close();
  110. }
  111. if(conn != null) {
  112. conn.disconnect();
  113. }
  114. }
  115. if(conn != null) {
  116. return readInputStream(conn.getInputStream());
  117. }
  118. return null;
  119. }
  120. }
  121. public static String post(String href, String data, int timeout) throws Exception {
  122. return post(href, data, timeout, "text/plain");
  123. }
  124. public static String post(String href, String data) throws Exception {
  125. return post(href, data, 0);
  126. }
  127. public static String postJson(String href, String data) throws Exception {
  128. return post(href, data, 0, "application/json;charset=utf-8");
  129. }
  130. public static String postForm(String href, String data, int timeout) throws Exception {
  131. return post(href, data, timeout, "application/x-www-form-urlencoded");
  132. }
  133. public static String postForm(String href, String data) throws Exception {
  134. return postForm(href, data, 0);
  135. }
  136. /**
  137. * 把map转换为url地址
  138. * @param server 项目地址
  139. * @param map 存储参数的map
  140. * @return 返回完整的url地址
  141. */
  142. public static String mapToUrl(String server, Map<String,String> map){
  143. StringBuilder url = new StringBuilder(server);
  144. StringBuilder param = new StringBuilder("?");
  145. if (map == null) {
  146. return url.toString();
  147. }
  148. for(Map.Entry<String,String> entry: map.entrySet()){
  149. String key = entry.getKey();
  150. String val = entry.getValue();
  151. if(key.equalsIgnoreCase("action")){
  152. url.append(val).append(".do");
  153. } else {
  154. param.append(key).append("=").append(val).append("&");
  155. }
  156. }
  157. //删去最后的&
  158. if (param.toString().endsWith("&")) {
  159. param.deleteCharAt(param.length() - 1);
  160. }
  161. url.append(param);
  162. return url.toString();
  163. }
  164. /**
  165. * 把url的参数转换成Map
  166. * @param param 参数串
  167. * @return 返回存储Map
  168. */
  169. public static Map<String, String> urlParamsToMap(String param) {
  170. Map<String, String> map = new HashMap<String, String>(0);
  171. if (StringUtils.isBlank(param)) {
  172. return map;
  173. }
  174. String[] params = param.split("&");
  175. for (int i = 0; i < params.length; i++) {
  176. String[] p = params[i].split("=");
  177. if (p.length == 2) {
  178. map.put(p[0], p[1]);
  179. }
  180. }
  181. return map;
  182. }
  183. public static void main(String[] args) throws Exception {
  184. String url = "http://114.255.201.228:86/mobile-report/sendAlarmMsg.do";
  185. String result = postForm(url, "name=CDN_BUSINESS&content=测试短信");
  186. System.out.println(result);
  187. }
  188. }