710856882b16e331b60909f664b5e0fedce23588.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.chinacreator.process.util;
  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.HashMap;
  8. import java.util.Set;
  9. public class URLUtil {
  10. /**
  11. * 解读流
  12. * @param inStream
  13. * @return
  14. * @throws Exception
  15. */
  16. public static String readInputStream(InputStream inStream) throws Exception {
  17. ByteArrayOutputStream outStream = null;
  18. try {
  19. outStream = new ByteArrayOutputStream();
  20. byte[] buffer = new byte[1024];
  21. int len = 0;
  22. while ((len = inStream.read(buffer)) != -1) {
  23. outStream.write(buffer, 0, len);
  24. }
  25. byte[] data = outStream.toByteArray();
  26. return new String(data, "utf-8");
  27. } finally{
  28. if(outStream != null)
  29. outStream.close();
  30. if(inStream != null)
  31. inStream.close();
  32. }
  33. }
  34. /**
  35. * 以get方式获取链接的输出
  36. * @param href
  37. * @return
  38. * @throws Exception
  39. */
  40. public static String get(String href, int timeout) throws Exception {
  41. HttpURLConnection conn = null;
  42. try {
  43. URL url = new URL(href);
  44. conn = (HttpURLConnection) url.openConnection();
  45. conn.setRequestMethod("GET");
  46. if(timeout > 0) {
  47. conn.setConnectTimeout(timeout);
  48. conn.setReadTimeout(timeout);
  49. }
  50. conn.addRequestProperty("content-type", "text/plain");
  51. conn.setUseCaches(false);
  52. conn.setDoOutput(false);
  53. return readInputStream(conn.getInputStream());
  54. } finally {
  55. if(conn != null) {
  56. conn.disconnect();
  57. }
  58. }
  59. }
  60. public static String get(String href) throws Exception {
  61. return get(href, 0);
  62. }
  63. public static String get(String href, HashMap<String,String> map) throws Exception {
  64. HttpURLConnection conn = null;
  65. try {
  66. URL url = new URL(href);
  67. conn = (HttpURLConnection) url.openConnection();
  68. conn.setRequestMethod("GET");
  69. conn.addRequestProperty("content-type", "text/plain");
  70. if (map!=null&&map.size()>0){
  71. Set<String> strings = map.keySet();
  72. for (String key:strings){
  73. conn.addRequestProperty(key,map.get(key));
  74. }
  75. }
  76. conn.setUseCaches(false);
  77. conn.setDoOutput(false);
  78. return readInputStream(conn.getInputStream());
  79. } finally {
  80. if(conn != null) {
  81. conn.disconnect();
  82. }
  83. }
  84. }
  85. /**
  86. * 以post方式获取链接的输出
  87. * @param href
  88. * @return
  89. * @throws Exception
  90. */
  91. public static String post(String href, String data, int timeout, String contentType) throws Exception {
  92. HttpURLConnection conn = null;
  93. OutputStreamWriter out = null;
  94. if(data == null || data.length() == 0) {
  95. return get(href, timeout);
  96. } else {
  97. try {
  98. URL url = new URL(href);
  99. if ("https".equalsIgnoreCase(url.getProtocol())) {
  100. SslUtils.ignoreSsl();
  101. }
  102. conn = (HttpURLConnection) url.openConnection();
  103. conn.setRequestMethod("POST");
  104. if(timeout > 0) {
  105. conn.setConnectTimeout(timeout);
  106. conn.setReadTimeout(timeout);
  107. }
  108. conn.addRequestProperty("content-type", contentType);
  109. conn.setDoOutput(true);
  110. conn.setUseCaches(false);
  111. out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  112. out.write(data);
  113. out.flush();
  114. } finally {
  115. if(out != null) {
  116. out.close();
  117. }
  118. if(conn != null) {
  119. conn.disconnect();
  120. }
  121. }
  122. if(conn != null) {
  123. return readInputStream(conn.getInputStream());
  124. }
  125. return null;
  126. }
  127. }
  128. public static String postJson(String href, String data) throws Exception {
  129. return post(href, data, 0, "application/json;charset=utf-8");
  130. }
  131. public static String postJson(String href, String data, int timeout) throws Exception {
  132. return post(href, data, timeout, "application/json;charset=utf-8");
  133. }
  134. public static String post(String href, String data, int timeout) throws Exception {
  135. return post(href, data, timeout, "text/plain");
  136. }
  137. public static String post(String href, String data) throws Exception {
  138. return post(href, data, 0);
  139. }
  140. public static String postForm(String href, String data, int timeout) throws Exception {
  141. return post(href, data, timeout, "application/x-www-form-urlencoded");
  142. }
  143. public static String postForm(String href, String data) throws Exception {
  144. return postForm(href, data, 0);
  145. }
  146. public static void main(String[] args) throws Exception {
  147. String url = "http://114.255.201.228:86/mobile-report/sendAlarmMsg.do";
  148. String result = postForm(url, "name=CDN_BUSINESS&content=测试短信");
  149. System.out.println(result);
  150. }
  151. }