QueryDemo_Test.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.*;
  7. import java.util.Iterator;
  8. /**
  9. * @author mashengyi
  10. */
  11. public class QueryDemo_Test {
  12. /**
  13. * 测试地址
  14. */
  15. private static final String URL = "http://127.0.0.1:18081/gateway?api=credit.sec.data";
  16. /**
  17. * 分配的appKey
  18. */
  19. private static final String APP_KEY = "jkxy";
  20. /**
  21. * 分配的appSecret
  22. */
  23. private static final String APP_SECRET = "7697CE9BB9D5A856CFDDE738320AD34EA53E483C";
  24. /**
  25. * 企业编号
  26. */
  27. private static final String COMPANY_NUM = "10004616";
  28. /**
  29. * 方法入口
  30. *
  31. * @param args
  32. */
  33. public static void main(String[] args) throws Exception {
  34. QueryDemo_Test demo = new QueryDemo_Test();
  35. demo.runMainService();
  36. }
  37. /**
  38. * 调用主接口
  39. */
  40. public void runMainService() {
  41. try {
  42. /**
  43. * 实名demo
  44. */
  45. JSONObject paramJsonObj = initParamJsonObjRealname();
  46. String ret = postClient(URL, paramJsonObj);
  47. System.out.println(ret);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. /**
  53. * 整合参数
  54. *
  55. * @return
  56. */
  57. public JSONObject initParamJsonObjRealname() {
  58. /**
  59. * 基本参数
  60. */
  61. JSONObject paramJsonObj = new JSONObject();
  62. paramJsonObj.put("api", "PROTOCOL_ADD_V1");//CMCC_MOBILE_CHECK_V8//CMCC_3RD_V2//CMCC_3RD_DETAIL_V1//
  63. paramJsonObj.put("appKey", APP_KEY);
  64. paramJsonObj.put("appSecret", APP_SECRET);
  65. /**
  66. * 业务参数
  67. */
  68. JSONObject dataJson = initMainServiceParamJsonObjRealname();
  69. paramJsonObj.put("data", dataJson);
  70. return paramJsonObj;
  71. }
  72. public JSONObject initMainServiceParamJsonObjRealname() {
  73. /**
  74. * 具体业务参数放在data中
  75. */
  76. JSONObject dataJson = new JSONObject();
  77. dataJson.put("companyNum", COMPANY_NUM);
  78. dataJson.put("serviceStartTime", "2022-05-11T17:30:37");
  79. dataJson.put("serviceEndTime", "2023-05-11T21:30:37");
  80. dataJson.put("contractFileName", "etc高速开票抵扣(1).pdf");
  81. dataJson.put("serviceType", "3");
  82. dataJson.put("base64Str", Base64Utils.fileToBase64Str(new File("/Users/mashengyi/Desktop/etc高速开票抵扣(1).pdf")));
  83. return dataJson;
  84. }
  85. /**
  86. * 实现http post请求 注意编码 UTF-8
  87. *
  88. * @param url
  89. * @param paramObj
  90. * @return
  91. */
  92. public String postClient(String url, JSONObject paramObj) {
  93. String str = "";
  94. HttpClient client = null;
  95. PostMethod method = null;
  96. try {
  97. client = new HttpClient();
  98. method = new PostMethod(url);
  99. method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  100. NameValuePair[] param = new NameValuePair[paramObj.size()];
  101. @SuppressWarnings("unchecked")
  102. Iterator<String> keys = paramObj.keys();
  103. int i = 0;
  104. while (keys.hasNext()) {
  105. String key = (String) keys.next();
  106. String value = paramObj.getString(key);
  107. param[i] = new NameValuePair(key, value);
  108. i++;
  109. }
  110. method.setRequestBody(param);
  111. client.executeMethod(method);
  112. str = convertStreamToString(method.getResponseBodyAsStream());
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. } finally {
  116. if (method != null) {
  117. method.releaseConnection();
  118. }
  119. }
  120. return str;
  121. }
  122. /**
  123. * 流转字符串
  124. *
  125. * @param is
  126. * @return
  127. */
  128. public static String convertStreamToString(InputStream is) {
  129. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  130. StringBuilder builder = new StringBuilder();
  131. String line = null;
  132. try {
  133. while ((line = reader.readLine()) != null) {
  134. builder.append(line + "\n");
  135. }
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. } finally {
  139. try {
  140. is.close();
  141. } catch (IOException e) {
  142. e.printStackTrace();
  143. }
  144. }
  145. return builder.toString();
  146. }
  147. }