QueryDemo_3rd3.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.LineNumberReader;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18. public class QueryDemo_3rd3 {
  19. /**
  20. * 测试地址
  21. */
  22. private static final String URL = "http://127.0.0.1:18081/api/rest";
  23. /**
  24. * 分配的appKey
  25. */
  26. private static final String appKey = "data_test";
  27. static String filename = "/Users/mashengyi/Downloads/";
  28. static String logname = "请求参数.txt";
  29. /**
  30. * 方法入口
  31. *
  32. * @param args
  33. */
  34. public static void main(String args[]) throws Exception {
  35. QueryDemo_3rd3 demo = new QueryDemo_3rd3();
  36. int count = 0;
  37. int total = 0;
  38. HashMap hashMap = new HashMap();
  39. try {
  40. File file = new File(filename + logname);
  41. FileInputStream fis = new FileInputStream(file);
  42. InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
  43. LineNumberReader lr = new LineNumberReader(isr, 512);
  44. String laststr = "";
  45. int count_d = 0;
  46. int total_d = 0;
  47. while (true) {
  48. String str = lr.readLine();
  49. try {
  50. if (str == null)
  51. break;
  52. String base64Str = str.replaceAll(" ", "+");
  53. System.out.println(base64Str);
  54. com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
  55. com.alibaba.fastjson.JSONObject jsonObject1 = jsonObject.getJSONObject("data");
  56. demo.runMainService(jsonObject.getString("waybillFileName"),jsonObject.getInteger("contractType"),jsonObject.getString("base64Str"));
  57. /** if(str.contains("18:")){
  58. continue;
  59. }
  60. if(str.contains("requestid")|| str.contains("未解密成功")){
  61. System.out.println(str);
  62. }
  63. if( str.contains("未解密成功")){
  64. System.out.println(str);
  65. continue;
  66. }
  67. String str1 = getsrt("\"msg\":\"", "\"", str);
  68. System.out.println(str1);**/
  69. // String mobile = getsrt("tradeIds=[", "]},", str).trim();
  70. // System.out.println(mobile);
  71. //String idcard = getsrt("idcard:", "name:", str).trim();
  72. //String name = getsrt1("name:",str).trim();
  73. //demo.runMainService(name,idcard,mobile);
  74. } catch (Exception e) {
  75. System.out.println("无法解析:" + str);
  76. }
  77. }
  78. lr.close();
  79. } catch (FileNotFoundException e) {
  80. System.out.println("FILENAME:" + filename);
  81. System.out.println("File not found");
  82. } catch (IOException e) {
  83. System.out.println("IO error");
  84. }
  85. Iterator it = hashMap.keySet().iterator();
  86. while (it.hasNext()) {
  87. String key = (String) it.next();
  88. Integer ttttttt = (Integer) hashMap.get(key);
  89. System.out.println(key + "====" + ttttttt);
  90. }
  91. }
  92. public static String getsrt(String startkeystr, String endstr, String str) {
  93. int customerindex = str.indexOf(startkeystr);
  94. String temp = str.substring(customerindex + startkeystr.length());
  95. int endindex = temp.indexOf(endstr);
  96. String sss = temp.substring(0, endindex);
  97. return sss;
  98. }
  99. public static String getsrt1(String startkeystr, String str) {
  100. int customerindex = str.indexOf(startkeystr);
  101. String temp = str.substring(customerindex + startkeystr.length());
  102. return temp;
  103. }
  104. /**
  105. * 利用正则表达式判断字符串是否是数字
  106. *
  107. * @param str
  108. * @return
  109. */
  110. public static boolean isNumeric(String str) {
  111. Pattern pattern = Pattern.compile("[0-9]*");
  112. Matcher isNum = pattern.matcher(str);
  113. if (!isNum.matches()) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. /**
  119. * 调用主接口
  120. */
  121. public void runMainService(String name,Integer idcard,String mobile) {
  122. try {
  123. /**
  124. * 实名demo
  125. */
  126. JSONObject paramJsonObj = initParamJsonObj_Realname(name,idcard,mobile);
  127. String ret = postClient(URL, paramJsonObj);
  128. System.out.println(ret);
  129. } catch (Exception e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. /**
  134. * 整合参数(实名认证)
  135. *
  136. * @return
  137. */
  138. public JSONObject initParamJsonObj_Realname(String name,Integer idcard,String mobile) {
  139. /**
  140. * 基本参数
  141. */
  142. JSONObject paramJsonObj = new JSONObject();
  143. paramJsonObj.put("api", "WAY_BILL_HISTORY_START");
  144. paramJsonObj.put("appKey", appKey);
  145. paramJsonObj.put("appSecret",
  146. "6MD50FA127P1ACNP9C64272X13C1FA1BEA20H406");
  147. /**
  148. * 业务参数
  149. */
  150. JSONObject dataJson = initMainServiceParamJsonObj_Realname(name,idcard,mobile);
  151. paramJsonObj.put("data", dataJson);
  152. return paramJsonObj;
  153. }
  154. /*
  155. * 初始化业务参数(实名认证)
  156. *
  157. * 姓名,身份证,手机号
  158. *
  159. * @return
  160. */
  161. public JSONObject initMainServiceParamJsonObj_Realname(String name,Integer idcard,String mobile) {
  162. /**
  163. * 具体业务参数放在data中
  164. */
  165. JSONObject dataJson = new JSONObject();
  166. dataJson.put("num", "1556502455-72");
  167. dataJson.put("plateNumber", "京CF0237");
  168. dataJson.put("plateColor", 1);
  169. dataJson.put("startTime", "2023-09-13T19:25:56");
  170. dataJson.put("sourceAddr", "北京市朝阳区");
  171. dataJson.put("destAddr", "北京市朝阳区");
  172. dataJson.put("predictEndTime", "2023-11-16T09:25:56");
  173. dataJson.put("fee", 10000);
  174. dataJson.put("titleType", 1);
  175. dataJson.put("taxplayerCode", "91330106MA28LMW9XQ");
  176. dataJson.put("waybillFileName", name);
  177. dataJson.put("base64Str", mobile);
  178. dataJson.put("contractType",idcard);
  179. return dataJson;
  180. }
  181. /**
  182. * 实现http post请求 注意编码 UTF-8
  183. *
  184. * @param url
  185. * @param paramObj
  186. * @return
  187. */
  188. public String postClient(String url, JSONObject paramObj) {
  189. String str = "";
  190. HttpClient client = null;
  191. PostMethod method = null;
  192. try {
  193. client = new HttpClient();
  194. method = new PostMethod(url);
  195. method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  196. NameValuePair[] param = new NameValuePair[paramObj.size()];
  197. Iterator<String> keys = paramObj.keys();
  198. int i = 0;
  199. while (keys.hasNext()) {
  200. String key = (String) keys.next();
  201. String value = paramObj.getString(key);
  202. param[i] = new NameValuePair(key, value);
  203. i++;
  204. }
  205. method.setRequestBody(param);
  206. client.executeMethod(method);
  207. str = convertStreamToString(method.getResponseBodyAsStream());
  208. } catch (Exception e) {
  209. e.printStackTrace();
  210. } finally {
  211. if (method != null) {
  212. method.releaseConnection();
  213. }
  214. }
  215. return str;
  216. }
  217. /**
  218. * 流转字符串
  219. *
  220. * @param is
  221. * @return
  222. */
  223. public static String convertStreamToString(InputStream is) {
  224. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  225. StringBuilder builder = new StringBuilder();
  226. String line = null;
  227. try {
  228. while ((line = reader.readLine()) != null) {
  229. builder.append(line + "\n");
  230. }
  231. } catch (IOException e) {
  232. e.printStackTrace();
  233. } finally {
  234. try {
  235. is.close();
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. }
  239. }
  240. return builder.toString();
  241. }
  242. }