package com.jkcredit.invoice.util; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.HashMap; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; public class QueryDemo_3rd3 { /** * 测试地址 */ private static final String URL = "http://127.0.0.1:18081/api/rest"; /** * 分配的appKey */ private static final String appKey = "data_test"; static String filename = "/Users/mashengyi/Downloads/"; static String logname = "请求参数.txt"; /** * 方法入口 * * @param args */ public static void main(String args[]) throws Exception { QueryDemo_3rd3 demo = new QueryDemo_3rd3(); int count = 0; int total = 0; HashMap hashMap = new HashMap(); try { File file = new File(filename + logname); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); LineNumberReader lr = new LineNumberReader(isr, 512); String laststr = ""; int count_d = 0; int total_d = 0; while (true) { String str = lr.readLine(); try { if (str == null) break; String base64Str = str.replaceAll(" ", "+"); System.out.println(base64Str); com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str); com.alibaba.fastjson.JSONObject jsonObject1 = jsonObject.getJSONObject("data"); demo.runMainService(jsonObject.getString("waybillFileName"),jsonObject.getInteger("contractType"),jsonObject.getString("base64Str")); /** if(str.contains("18:")){ continue; } if(str.contains("requestid")|| str.contains("未解密成功")){ System.out.println(str); } if( str.contains("未解密成功")){ System.out.println(str); continue; } String str1 = getsrt("\"msg\":\"", "\"", str); System.out.println(str1);**/ // String mobile = getsrt("tradeIds=[", "]},", str).trim(); // System.out.println(mobile); //String idcard = getsrt("idcard:", "name:", str).trim(); //String name = getsrt1("name:",str).trim(); //demo.runMainService(name,idcard,mobile); } catch (Exception e) { System.out.println("无法解析:" + str); } } lr.close(); } catch (FileNotFoundException e) { System.out.println("FILENAME:" + filename); System.out.println("File not found"); } catch (IOException e) { System.out.println("IO error"); } Iterator it = hashMap.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); Integer ttttttt = (Integer) hashMap.get(key); System.out.println(key + "====" + ttttttt); } } public static String getsrt(String startkeystr, String endstr, String str) { int customerindex = str.indexOf(startkeystr); String temp = str.substring(customerindex + startkeystr.length()); int endindex = temp.indexOf(endstr); String sss = temp.substring(0, endindex); return sss; } public static String getsrt1(String startkeystr, String str) { int customerindex = str.indexOf(startkeystr); String temp = str.substring(customerindex + startkeystr.length()); return temp; } /** * 利用正则表达式判断字符串是否是数字 * * @param str * @return */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (!isNum.matches()) { return false; } return true; } /** * 调用主接口 */ public void runMainService(String name,Integer idcard,String mobile) { try { /** * 实名demo */ JSONObject paramJsonObj = initParamJsonObj_Realname(name,idcard,mobile); String ret = postClient(URL, paramJsonObj); System.out.println(ret); } catch (Exception e) { e.printStackTrace(); } } /** * 整合参数(实名认证) * * @return */ public JSONObject initParamJsonObj_Realname(String name,Integer idcard,String mobile) { /** * 基本参数 */ JSONObject paramJsonObj = new JSONObject(); paramJsonObj.put("api", "WAY_BILL_HISTORY_START"); paramJsonObj.put("appKey", appKey); paramJsonObj.put("appSecret", "6MD50FA127P1ACNP9C64272X13C1FA1BEA20H406"); /** * 业务参数 */ JSONObject dataJson = initMainServiceParamJsonObj_Realname(name,idcard,mobile); paramJsonObj.put("data", dataJson); return paramJsonObj; } /* * 初始化业务参数(实名认证) * * 姓名,身份证,手机号 * * @return */ public JSONObject initMainServiceParamJsonObj_Realname(String name,Integer idcard,String mobile) { /** * 具体业务参数放在data中 */ JSONObject dataJson = new JSONObject(); dataJson.put("num", "1556502455-72"); dataJson.put("plateNumber", "京CF0237"); dataJson.put("plateColor", 1); dataJson.put("startTime", "2023-09-13T19:25:56"); dataJson.put("sourceAddr", "北京市朝阳区"); dataJson.put("destAddr", "北京市朝阳区"); dataJson.put("predictEndTime", "2023-11-16T09:25:56"); dataJson.put("fee", 10000); dataJson.put("titleType", 1); dataJson.put("taxplayerCode", "91330106MA28LMW9XQ"); dataJson.put("waybillFileName", name); dataJson.put("base64Str", mobile); dataJson.put("contractType",idcard); return dataJson; } /** * 实现http post请求 注意编码 UTF-8 * * @param url * @param paramObj * @return */ public String postClient(String url, JSONObject paramObj) { String str = ""; HttpClient client = null; PostMethod method = null; try { client = new HttpClient(); method = new PostMethod(url); method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); NameValuePair[] param = new NameValuePair[paramObj.size()]; Iterator keys = paramObj.keys(); int i = 0; while (keys.hasNext()) { String key = (String) keys.next(); String value = paramObj.getString(key); param[i] = new NameValuePair(key, value); i++; } method.setRequestBody(param); client.executeMethod(method); str = convertStreamToString(method.getResponseBodyAsStream()); } catch (Exception e) { e.printStackTrace(); } finally { if (method != null) { method.releaseConnection(); } } return str; } /** * 流转字符串 * * @param is * @return */ public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder builder = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { builder.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return builder.toString(); } }