QueryDemo_3rd.java 6.1 KB

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