QueryDemo_Test.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.util.Iterator;
  11. /**
  12. * Created by zhangqingxin Date : 16/11/6 Time : 10:41
  13. * <p/>
  14. * 参考一(HttpClient):http://mvnrepository.com/artifact/commons-httpclient/commons-
  15. * httpclient/3.1
  16. * 参考一(json-lib):http://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4
  17. */
  18. public class QueryDemo_Test {
  19. /**
  20. * 测试地址
  21. */
  22. // private static final String URL = "http://110.88.150.74:80/credit?api=credit.sec.data";
  23. //private static final String URL = "http://110.88.150.74/credit?api=credit.sec.data";
  24. // private static final String URL = "http://123.57.186.204/gateway?api=credit.sec.data";
  25. //private static final String URL = "http://123.57.186.204/gateway?api=credit.sec.data";
  26. //private static final String URL = "http://192.168.50.4:18080/gateway?api=credit.sec.data";//北京节点
  27. private static final String URL = "https://credit.h11.site/gateway?api=credit.sec.data";
  28. // private static final String URL = "http://127.0.0.1:18080/gateway?api=credit.sec.data";
  29. // private static final String URL = "http://110.88.150.68:8000/gateway?api=credit.sec.data";
  30. // private static final String URL = "http://110.88.150.72:8000/gateway?api=credit.sec.data_test";
  31. // private static final String URL = "http://60.205.114.163:8000/gateway?api=credit.sec.data";
  32. //private static final String URL = " http://45.126.120.88/gateway?api=credit.sec.data_test";
  33. // private static final String URL = " http://45.126.120.88/gateway?api=credit.sec.data";
  34. // private static final String URL = " http://119.18.195.163/gateway?api=credit.sec.data";
  35. /**
  36. * 分配的appKey
  37. */
  38. // private static final String APP_KEY = "jkxy";
  39. // private static final String appKey = "junxin_test";
  40. private static final String appKey = "DATA_TEST";
  41. //private static final String appKey = "JIAO_KE";
  42. //private static final String appKey = "HANGZHOU_JUNXIN_72";
  43. /**290dec3f6a243889e5ed3210cf1ad499
  44. * 方法入口
  45. *
  46. * @param args
  47. */
  48. public static void main(String args[]) throws Exception {
  49. QueryDemo_Test demo = new QueryDemo_Test();
  50. demo.runMainService();
  51. }
  52. /**
  53. * 调用主接口
  54. */
  55. public void runMainService() {
  56. try {
  57. /**
  58. * 实名demo
  59. */
  60. JSONObject paramJsonObj = initParamJsonObj_Realname();
  61. String ret = postClient(URL, paramJsonObj);
  62. System.out.println(ret);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. /**
  68. * 整合参数(实名认证)
  69. *
  70. * @return
  71. */
  72. public JSONObject initParamJsonObj_Realname() {
  73. /**
  74. * 基本参数
  75. */
  76. JSONObject paramJsonObj = new JSONObject();
  77. paramJsonObj.put("api", "CMCC_STATUS_CHECK_V7");//CMCC_3RD_DETAIL_V15//CMCC_2ND_V27G//CMCC_3RD_DETAIL_V32B//CMCC_ONLINE_CHECK_V8//CMCC_STATUS_CHECK_V7
  78. paramJsonObj.put("appKey", appKey);
  79. paramJsonObj.put("appSecret",
  80. // "D78C393359BF128715C65D91C67051478A4DFC13");
  81. "C41DF125F0C23FBBB83E461F5045A30ACB3FF55A");
  82. /**
  83. * 业务参数
  84. */
  85. JSONObject dataJson = initMainServiceParamJsonObj_Realname();
  86. paramJsonObj.put("data", dataJson);
  87. return paramJsonObj;
  88. }
  89. /*
  90. * 初始化业务参数(实名认证)
  91. *
  92. * 姓名,身份证,手机号
  93. *
  94. * @return
  95. */
  96. public JSONObject initMainServiceParamJsonObj_Realname() {
  97. /**
  98. * 具体业务参数放在data中
  99. */
  100. JSONObject dataJson = new JSONObject();
  101. dataJson.put("name","陈春灵");
  102. dataJson.put("mobile","19846930551");
  103. dataJson.put("id_number","372926198502202847");
  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. }