QueryDemoTest_opera.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_opera {
  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_opera demo = new QueryDemoTest_opera();
  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_OPERATORVALIDCODE");
  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", "91111205MA01BCJJ8U");
  103. dataJson.put("validCode", "122222");
  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. }