QueryDemo_Test.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * Created by zhangqingxin Date : 16/11/6 Time : 10:41
  10. * <p/>
  11. * 参考一(HttpClient):http://mvnrepository.com/artifact/commons-httpclient/commons-
  12. * httpclient/3.1
  13. * 参考一(json-lib):http://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4
  14. */
  15. public class QueryDemo_Test {
  16. /**
  17. * 测试地址
  18. */
  19. private static final String URL = "http://etc.jkcredit.com:9999/api/rest";
  20. //private static final String URL = "http://110.88.150.74:80/credit?api=credit.sec.data_test";
  21. //private static final String URL = "http://110.88.150.74/credit?api=credit.sec.data";
  22. //private static final String URL = "http://123.57.186.204/gateway?api=credit.sec.data_test";
  23. //private static final String URL = "http://123.57.186.204/gateway?api=credit.sec.data";
  24. // private static final String URL = "http://www1.h11.site/gateway?api=credit.sec.data";
  25. // private static final String URL = "http://110.88.150.68:8000/gateway?api=credit.sec.data";
  26. //private static final String URL = "http://60.205.114.163:8000/gateway?api=credit.sec.data";
  27. //private static final String URL = " http://45.126.120.88/gateway?api=credit.sec.data";
  28. //private static final String URL = "http://119.18.195.163/gateway?api=credit.sec.data";
  29. /**
  30. * 分配的appKey
  31. */
  32. //private static final String appKey = "junxin_test";
  33. private static final String appKey = "jkxy";
  34. //private static final String appKey = "ccx";
  35. /**
  36. * 方法入口
  37. *
  38. * @param args
  39. */
  40. public static void main(String args[]) throws Exception {
  41. QueryDemo_Test demo = new QueryDemo_Test();
  42. demo.runMainService();
  43. }
  44. /**
  45. * 调用主接口
  46. */
  47. public void runMainService() {
  48. try {
  49. /**
  50. * 实名demo
  51. */
  52. JSONObject paramJsonObj = initParamJsonObj_Realname();
  53. String ret = postClient(URL, paramJsonObj);
  54. System.out.println(ret);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. /**
  60. * 整合参数(实名认证)
  61. *
  62. * @return
  63. */
  64. public JSONObject initParamJsonObj_Realname() {
  65. /**
  66. * 基本参数
  67. */
  68. JSONObject paramJsonObj = new JSONObject();
  69. paramJsonObj.put("api", "RED_INK_INVOICE_QUERY");//CTCC_3RD_DETAIL_V11//CTCC_STATUS_CHECK_V11//CUCC_STATUS_CHECK_V11//CMCC_3RD_DETAIL_V15//CMCC_3RD_V2//CTCC_3RD_DETAIL_V11//
  70. //paramJsonObj.put("api", "MSISDNMD5TOIMEI");//MOBILE_CHECK_V1//CMCC_3RD_VERIFY_V4//CTCC_CHECK_V1
  71. paramJsonObj.put("appKey", appKey);
  72. paramJsonObj.put("appSecret",
  73. "7697CE9BB9D5A856CFDDE738320AD34EA53E483C");
  74. //"84C1CE24EDF361F28072E313BD87EAB24CC727CF");
  75. /**
  76. * 业务参数
  77. */
  78. JSONObject dataJson = initMainServiceParamJsonObj_Realname();
  79. paramJsonObj.put("data", dataJson);
  80. return paramJsonObj;
  81. }
  82. /*
  83. * 初始化业务参数(实名认证)
  84. *
  85. * 姓名,身份证,手机号
  86. *
  87. * @return
  88. */
  89. public JSONObject initMainServiceParamJsonObj_Realname() {
  90. /**
  91. * 具体业务参数放在data中
  92. */
  93. JSONObject dataJson = new JSONObject();
  94. /* dataJson.put("name", "王同");
  95. dataJson.put("id_number","371081198911276112");
  96. dataJson.put("mobile","15562139518");*/
  97. dataJson.put("taxplayerCode", "91149900MA0LBCKM6F");
  98. dataJson.put("month", "2022-04");
  99. dataJson.put("pageNo", 4);
  100. dataJson.put("pageSize",10); //没有就默认1000
  101. /* List<String> list = new ArrayList<>();
  102. list.add("13752639577");
  103. dataJson.put("msisdnmd5list",list);*/
  104. return dataJson;
  105. }
  106. /**
  107. * 实现http post请求 注意编码 UTF-8
  108. *
  109. * @param url
  110. * @param paramObj
  111. * @return
  112. */
  113. public String postClient(String url, JSONObject paramObj) {
  114. String str = "";
  115. HttpClient client = null;
  116. PostMethod method = null;
  117. try {
  118. client = new HttpClient();
  119. method = new PostMethod(url);
  120. method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  121. NameValuePair[] param = new NameValuePair[paramObj.size()];
  122. @SuppressWarnings("unchecked")
  123. Iterator<String> keys = paramObj.keys();
  124. int i = 0;
  125. while (keys.hasNext()) {
  126. String key = (String) keys.next();
  127. String value = paramObj.getString(key);
  128. param[i] = new NameValuePair(key, value);
  129. i++;
  130. }
  131. method.setRequestBody(param);
  132. client.executeMethod(method);
  133. str = convertStreamToString(method.getResponseBodyAsStream());
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. } finally {
  137. if (method != null) {
  138. method.releaseConnection();
  139. }
  140. }
  141. return str;
  142. }
  143. /**
  144. * 流转字符串
  145. *
  146. * @param is
  147. * @return
  148. */
  149. public static String convertStreamToString(InputStream is) {
  150. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  151. StringBuilder builder = new StringBuilder();
  152. String line = null;
  153. try {
  154. while ((line = reader.readLine()) != null) {
  155. builder.append(line + "\n");
  156. }
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. } finally {
  160. try {
  161. is.close();
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. return builder.toString();
  167. }
  168. }