HttpUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package cn.com.lzt.common.util;
  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.net.ConnectException;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.util.Vector;
  11. import com.alibaba.fastjson.JSONObject;
  12. public class HttpUtil {
  13. /**
  14. * 发起https请求并获取结果
  15. *
  16. * @param requestUrl
  17. * 请求地址
  18. * @param requestMethod
  19. * 请求方式(GET、POST)
  20. * @param outputStr
  21. * 提交的数据
  22. * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
  23. */
  24. public static JSONObject httpRequest(String requestUrl,
  25. String requestMethod, String outputStr) {
  26. JSONObject jsonObject = null;
  27. StringBuffer buffer = new StringBuffer();
  28. HttpURLConnection httpUrlConn = null;
  29. try {
  30. // 创建SSLContext对象,并使用我们指定的信任管理器初始化
  31. URL url = new URL(requestUrl);
  32. httpUrlConn = (HttpURLConnection) url.openConnection();
  33. httpUrlConn.setDoOutput(true);
  34. httpUrlConn.setDoInput(true);
  35. httpUrlConn.setUseCaches(false);
  36. httpUrlConn.setRequestProperty("Content-Type",
  37. "text/plain;charset=UTF-8");
  38. // httpUrlConn.setRequestProperty("content-type", "text/html");
  39. // 设置请求方式(GET/POST)
  40. httpUrlConn.setRequestMethod(requestMethod);
  41. if ("GET".equalsIgnoreCase(requestMethod))
  42. httpUrlConn.connect();
  43. // 当有数据需要提交时
  44. if (null != outputStr) {
  45. OutputStream outputStream = httpUrlConn.getOutputStream();
  46. // 注意编码格式,防止中文乱码
  47. outputStream.write(outputStr.getBytes("UTF-8"));
  48. outputStream.close();
  49. }
  50. // 将返回的输入流转换成字符串
  51. InputStream inputStream = httpUrlConn.getInputStream();
  52. InputStreamReader inputStreamReader = new InputStreamReader(
  53. inputStream, "utf-8");
  54. BufferedReader bufferedReader = new BufferedReader(
  55. inputStreamReader);
  56. String str = null;
  57. while ((str = bufferedReader.readLine()) != null) {
  58. buffer.append(str);
  59. }
  60. bufferedReader.close();
  61. inputStreamReader.close();
  62. // 释放资源
  63. inputStream.close();
  64. inputStream = null;
  65. httpUrlConn.disconnect();
  66. jsonObject = JSONObject.parseObject(buffer.toString());
  67. // jsonObject = JSONObject.fromObject(buffer.toString());
  68. } catch (ConnectException ce) {
  69. org.jeecgframework.core.util.LogUtil
  70. .info("Weixin server connection timed out.");
  71. } catch (Exception e) {
  72. //e.printStackTrace();
  73. org.jeecgframework.core.util.LogUtil.info("https request error:{}"
  74. + e.getMessage());
  75. }finally{
  76. try {
  77. httpUrlConn.disconnect();
  78. }catch (Exception e) {
  79. e.printStackTrace();
  80. org.jeecgframework.core.util.LogUtil.info("http close error:{}"+ e.getMessage());
  81. }
  82. }
  83. return jsonObject;
  84. }
  85. public static HttpRespons post(String strURL,String params){
  86. try {
  87. HttpRespons httpResponser = new HttpRespons();
  88. URL url = new URL(strURL);// 创建连接
  89. HttpURLConnection connection = (HttpURLConnection) url
  90. .openConnection();
  91. connection.setDoOutput(true);
  92. connection.setDoInput(true);
  93. connection.setUseCaches(false);
  94. connection.setInstanceFollowRedirects(true);
  95. connection.setRequestMethod("POST"); // 设置请求方式
  96. connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
  97. connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
  98. connection.connect();
  99. OutputStreamWriter out = new OutputStreamWriter(
  100. connection.getOutputStream(), "UTF-8"); // utf-8编码
  101. out.append(params);
  102. out.flush();
  103. out.close();
  104. // 读取响应
  105. InputStream is = connection.getInputStream();
  106. BufferedReader bufferedReader = new BufferedReader(
  107. new InputStreamReader(is));
  108. httpResponser.contentCollection = new Vector<String>();
  109. StringBuffer temp = new StringBuffer();
  110. String line = bufferedReader.readLine();
  111. while (line != null) {
  112. httpResponser.contentCollection.add(line);
  113. temp.append(line).append("\r\n");
  114. line = bufferedReader.readLine();
  115. }
  116. bufferedReader.close();
  117. httpResponser.urlString = strURL;
  118. httpResponser.defaultPort = connection.getURL().getDefaultPort();
  119. httpResponser.file = connection.getURL().getFile();
  120. httpResponser.host = connection.getURL().getHost();
  121. httpResponser.path = connection.getURL().getPath();
  122. httpResponser.port = connection.getURL().getPort();
  123. httpResponser.protocol = connection.getURL().getProtocol();
  124. httpResponser.query = connection.getURL().getQuery();
  125. httpResponser.ref = connection.getURL().getRef();
  126. httpResponser.userInfo = connection.getURL().getUserInfo();
  127. httpResponser.content = new String(temp.toString().getBytes(),
  128. "UTF-8");
  129. httpResponser.contentEncoding = "UTF-8";
  130. httpResponser.code = connection.getResponseCode();
  131. httpResponser.message = connection.getResponseMessage();
  132. httpResponser.contentType = connection.getContentType();
  133. httpResponser.method = connection.getRequestMethod();
  134. httpResponser.connectTimeout = connection.getConnectTimeout();
  135. httpResponser.readTimeout = connection.getReadTimeout();
  136. return httpResponser;
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. return null;
  140. }
  141. }
  142. public static void main(String args[]){
  143. String basePath = "http://localhost:8080/hglserver/";
  144. // 修改密码
  145. /*basePath = basePath + "sys/changePwd.do";
  146. SysDefaultReq req = new SysDefaultReq();
  147. req.setUserId("8a8ab0b246dc81120146dc8181a60055");
  148. req.setPwd("a324509dc1a3089a");
  149. req.setNewpwd("aaaaaaaaa");*/
  150. // ---------------------------------------------------
  151. // System.out.println("请求:" + JSON.toJSONString(req));
  152. // System.out.println("响应:" + post(basePath, JSON.toJSONString(req)).getContent());
  153. }
  154. }