QueryDemoTest_opera.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_opera {
  16. /**
  17. * 测试地址
  18. */
  19. private static final String URL = "http://127.0.0.1:18081/api/rest";
  20. /**
  21. * 分配的appKey
  22. */
  23. private static final String APP_KEY = "jkxy";
  24. /**
  25. * 分配的appSecret
  26. */
  27. private static final String APP_SECRET = "7697CE9BB1D5AC46CFDDA238328AD34EA33E483G";
  28. /**
  29. * 方法入口
  30. *
  31. * @param args
  32. */
  33. public static void main(String[] args) throws Exception {
  34. QueryDemoTest_opera demo = new QueryDemoTest_opera();
  35. demo.runMainService();
  36. }
  37. /**
  38. * 流转字符串
  39. *
  40. * @param is
  41. * @return
  42. */
  43. public static String convertStreamToString(InputStream is) {
  44. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  45. StringBuilder builder = new StringBuilder();
  46. String line = null;
  47. try {
  48. while ((line = reader.readLine()) != null) {
  49. builder.append(line + "\n");
  50. }
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. } finally {
  54. try {
  55. is.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. return builder.toString();
  61. }
  62. /**
  63. * 调用主接口
  64. */
  65. public void runMainService() {
  66. try {
  67. /**
  68. * 实名demo
  69. */
  70. JSONObject paramJsonObj = initParamJsonObjRealname();
  71. String ret = postClient(URL, paramJsonObj);
  72. System.out.println(ret);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. /**
  78. * 整合参数
  79. *
  80. * @return
  81. */
  82. public JSONObject initParamJsonObjRealname() {
  83. /**
  84. * 基本参数
  85. */
  86. JSONObject paramJsonObj = new JSONObject();
  87. //CMCC_MOBILE_CHECK_V8//CMCC_3RD_V2//CMCC_3RD_DETAIL_V1//
  88. paramJsonObj.put("api", "COMPANY_COMPANYDETAILADD");
  89. paramJsonObj.put("appKey", APP_KEY);
  90. paramJsonObj.put("appSecret", APP_SECRET);
  91. /**
  92. * 业务参数
  93. */
  94. JSONObject dataJson = initMainServiceParamJsonObjRealname();
  95. paramJsonObj.put("data", dataJson);
  96. return paramJsonObj;
  97. }
  98. public JSONObject initMainServiceParamJsonObjRealname() {
  99. /**
  100. * 具体业务参数放在data中
  101. */
  102. JSONObject dataJson = new JSONObject();
  103. dataJson.put("taxplayerCode", "91110105MA01BCJJ8U");
  104. dataJson.put("operatorName", "张三");
  105. dataJson.put("operatorMobile", "19910751229");
  106. dataJson.put("identificationId", "410327199311279656");
  107. dataJson.put("contractFileName", "测试msy.pdf");
  108. dataJson.put("base64Str", Base64Util.getPdfBinary(new File("/Users/mashengyi/Downloads/b10852e4-430a-4222-b088-41f12d41b941.pdf")));
  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. }