HttpUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package test;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.jeecgframework.web.cgform.util.SignatureUtil;
  4. import java.io.BufferedReader;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.net.ConnectException;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. public class HttpUtil {
  14. /**
  15. * 发起https请求并获取结果
  16. *
  17. * @param requestUrl
  18. * 请求地址
  19. * @param requestMethod
  20. * 请求方式(GET、POST)
  21. * @param outputStr
  22. * 提交的数据
  23. * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
  24. */
  25. public static JSONObject httpRequest(String requestUrl,
  26. String requestMethod, String outputStr,String sign) {
  27. JSONObject jsonObject = null;
  28. StringBuffer buffer = new StringBuffer();
  29. HttpURLConnection httpUrlConn = null;
  30. try {
  31. // 创建SSLContext对象,并使用我们指定的信任管理器初始化
  32. URL url = new URL(requestUrl);
  33. httpUrlConn = (HttpURLConnection) url.openConnection();
  34. httpUrlConn.setDoOutput(true);
  35. httpUrlConn.setDoInput(true);
  36. httpUrlConn.setUseCaches(false);
  37. httpUrlConn.setRequestProperty("X-JEECG-SIGN",sign);
  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 void main(String args[]){
  86. String key="26F72780372E84B6CFAED6F7B19139CC47B1912B6CAED753";
  87. JSONObject jsonObject=new JSONObject();
  88. jsonObject.put("id","40281381537e969401537eb9902d0006");
  89. jsonObject.put("tableName","jform_contact");
  90. String body=jsonObject.toJSONString();
  91. Map param=new HashMap();
  92. param.put("body",body);
  93. String sign=SignatureUtil.sign(param,key);
  94. System.out.println(" -- sign -- "+ sign);
  95. JSONObject resp=HttpUtil.httpRequest("http://localhost:8080/jeecg/api/cgFormDataController.do?getFormInfo","POST","body="+body,sign);
  96. System.out.println(resp.toJSONString());
  97. }
  98. public static void main2(String args[]){
  99. String key="26F72780372E84B6CFAED6F7B19139CC47B1912B6CAED753";
  100. JSONObject jsonObject=new JSONObject();
  101. jsonObject.put("id","40281381537e969401537eb9902d0006");
  102. jsonObject.put("tableName","jform_contact");
  103. JSONObject data=new JSONObject();
  104. data.put("name","张山丰");
  105. data.put("sex","0");
  106. jsonObject.put("data",data);
  107. String body=jsonObject.toJSONString();
  108. Map param=new HashMap();
  109. param.put("body",body);
  110. String sign=SignatureUtil.sign(param,key);
  111. System.out.println(" -- sign -- "+ sign);
  112. JSONObject resp=HttpUtil.httpRequest("http://localhost:8080/jeecg/api/cgFormDataController.do?addFormInfo","POST","body="+body,sign);
  113. System.out.println(resp.toJSONString());
  114. }
  115. }