9481724f8b8856f7e3207d16f5d30c1cc71e4d12.svn-base 4.7 KB

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