QueryDemoTest_end.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.jkcredit.invoice.util;
  2. import net.sf.json.JSONObject;
  3. import org.apache.commons.httpclient.HttpClient;
  4. import org.apache.commons.httpclient.NameValuePair;
  5. import org.apache.commons.httpclient.methods.PostMethod;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.util.Iterator;
  12. /**
  13. * @author mashengyi
  14. */
  15. public class QueryDemoTest_end {
  16. /**
  17. * 测试地址
  18. */
  19. private static final String URL = "http://192.168.50.13:18081/api/rest";
  20. /**
  21. * 分配的appKey
  22. */
  23. private static final String APP_KEY = "data_test1";
  24. /**
  25. * 分配的appSecret
  26. */
  27. private static final String APP_SECRET = "C1C2693835F1D2371085C044B5D25E624876BF39";
  28. static String filename = "/Users/mashengyi/Downloads/";
  29. static String logname = "test.pdf";
  30. File filePath = new File(filename + logname);
  31. /**
  32. * 方法入口
  33. *
  34. * @param args
  35. */
  36. public static void main(String[] args) throws Exception {
  37. QueryDemoTest_end demo = new QueryDemoTest_end();
  38. demo.runMainService();
  39. }
  40. /**
  41. * 流转字符串
  42. *
  43. * @param is
  44. * @return
  45. */
  46. public static String convertStreamToString(InputStream is) {
  47. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  48. StringBuilder builder = new StringBuilder();
  49. String line = null;
  50. try {
  51. while ((line = reader.readLine()) != null) {
  52. builder.append(line + "\n");
  53. }
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. } finally {
  57. try {
  58. is.close();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. return builder.toString();
  64. }
  65. /**
  66. * 调用主接口
  67. */
  68. public void runMainService() {
  69. try {
  70. /**
  71. * 实名demo
  72. */
  73. JSONObject paramJsonObj = initParamJsonObjRealname();
  74. String ret = postClient(URL, paramJsonObj);
  75. System.out.println(ret);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. /**
  81. * 整合参数
  82. *
  83. * @return
  84. */
  85. public JSONObject initParamJsonObjRealname() {
  86. /**
  87. * 基本参数
  88. */
  89. JSONObject paramJsonObj = new JSONObject();
  90. //CMCC_MOBILE_CHECK_V8//CMCC_3RD_V2//CMCC_3RD_DETAIL_V1//
  91. paramJsonObj.put("api", "WAY_BILL_END");
  92. paramJsonObj.put("appKey", APP_KEY);
  93. paramJsonObj.put("appSecret", APP_SECRET);
  94. /**
  95. * 业务参数
  96. */
  97. JSONObject dataJson = initMainServiceParamJsonObjRealname();
  98. paramJsonObj.put("data", dataJson);
  99. return paramJsonObj;
  100. }
  101. public JSONObject initMainServiceParamJsonObjRealname() {
  102. /**
  103. * 具体业务参数放在data中
  104. */
  105. JSONObject dataJson = new JSONObject();
  106. dataJson.put("num", "Zy20240918100211001");
  107. dataJson.put("realDestAddr", "北京海淀区周山路街道");
  108. dataJson.put("endTime", "2024-12-16T09:18:22");
  109. return dataJson;
  110. }
  111. /**
  112. * 实现http post请求 注意编码 UTF-8
  113. *
  114. * @param url
  115. * @param paramObj
  116. * @return
  117. */
  118. public String postClient(String url, JSONObject paramObj) {
  119. String str = "";
  120. HttpClient client = null;
  121. PostMethod method = null;
  122. try {
  123. client = new HttpClient();
  124. method = new PostMethod(url);
  125. method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  126. NameValuePair[] param = new NameValuePair[paramObj.size()];
  127. @SuppressWarnings("unchecked")
  128. Iterator<String> keys = paramObj.keys();
  129. int i = 0;
  130. while (keys.hasNext()) {
  131. String key = keys.next();
  132. String value = paramObj.getString(key);
  133. param[i] = new NameValuePair(key, value);
  134. i++;
  135. }
  136. method.setRequestBody(param);
  137. client.executeMethod(method);
  138. str = convertStreamToString(method.getResponseBodyAsStream());
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. } finally {
  142. if (method != null) {
  143. method.releaseConnection();
  144. }
  145. }
  146. return str;
  147. }
  148. }