|
@@ -0,0 +1,169 @@
|
|
|
+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.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.util.Iterator;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author mashengyi
|
|
|
+ */
|
|
|
+public class QueryDemoTest_end {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试地址
|
|
|
+ */
|
|
|
+ private static final String URL = "http://192.168.50.13:18081/api/rest";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分配的appKey
|
|
|
+ */
|
|
|
+ private static final String APP_KEY = "data_test1";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分配的appSecret
|
|
|
+ */
|
|
|
+ private static final String APP_SECRET = "C1C2693835F1D2371085C044B5D25E624876BF39";
|
|
|
+
|
|
|
+ static String filename = "/Users/mashengyi/Downloads/";
|
|
|
+ static String logname = "test.pdf";
|
|
|
+
|
|
|
+ File filePath = new File(filename + logname);
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法入口
|
|
|
+ *
|
|
|
+ * @param args
|
|
|
+ */
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ QueryDemoTest_end demo = new QueryDemoTest_end();
|
|
|
+ demo.runMainService();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 流转字符串
|
|
|
+ *
|
|
|
+ * @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();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用主接口
|
|
|
+ */
|
|
|
+ public void runMainService() {
|
|
|
+ try {
|
|
|
+ /**
|
|
|
+ * 实名demo
|
|
|
+ */
|
|
|
+ JSONObject paramJsonObj = initParamJsonObjRealname();
|
|
|
+ String ret = postClient(URL, paramJsonObj);
|
|
|
+ System.out.println(ret);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 整合参数
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject initParamJsonObjRealname() {
|
|
|
+ /**
|
|
|
+ * 基本参数
|
|
|
+ */
|
|
|
+ JSONObject paramJsonObj = new JSONObject();
|
|
|
+ //CMCC_MOBILE_CHECK_V8//CMCC_3RD_V2//CMCC_3RD_DETAIL_V1//
|
|
|
+ paramJsonObj.put("api", "WAY_BILL_END");
|
|
|
+ paramJsonObj.put("appKey", APP_KEY);
|
|
|
+ paramJsonObj.put("appSecret", APP_SECRET);
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 业务参数
|
|
|
+ */
|
|
|
+ JSONObject dataJson = initMainServiceParamJsonObjRealname();
|
|
|
+ paramJsonObj.put("data", dataJson);
|
|
|
+ return paramJsonObj;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject initMainServiceParamJsonObjRealname() {
|
|
|
+ /**
|
|
|
+ * 具体业务参数放在data中
|
|
|
+ */
|
|
|
+ JSONObject dataJson = new JSONObject();
|
|
|
+ dataJson.put("num", "Zy20240918100211001");
|
|
|
+ dataJson.put("realDestAddr", "北京海淀区周山路街道");
|
|
|
+
|
|
|
+ dataJson.put("endTime", "2024-12-16T09:18:22");
|
|
|
+
|
|
|
+
|
|
|
+ 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/json;charset=utf-8");
|
|
|
+ NameValuePair[] param = new NameValuePair[paramObj.size()];
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Iterator<String> keys = paramObj.keys();
|
|
|
+ int i = 0;
|
|
|
+ while (keys.hasNext()) {
|
|
|
+ String key = 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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|