QueryDemoTest.java 4.1 KB

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