|
|
package com.sunyo.energy.location.utils;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.HttpStatus;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.client.methods.HttpPut;
|
|
|
import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EncodingUtils;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
public class HttpsUtils {
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -60,7 +74,7 @@ public class HttpsUtils { |
|
|
* @return
|
|
|
*/
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
public static String urlencode(Map<String, Object> data) {
|
|
|
public static String urlencode(Map<String, Object> data) throws UnsupportedEncodingException {
|
|
|
// 将map里的参数变成像 showapi_appid=###&showapi_sign=###&的样子
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (Map.Entry i : data.entrySet()) {
|
...
|
...
|
@@ -68,6 +82,7 @@ public class HttpsUtils { |
|
|
sb.append(i.getKey()).append("=")
|
|
|
.append(URLEncoder.encode(i.getValue() + "", "UTF-8"))
|
|
|
.append("&");
|
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
...
|
...
|
@@ -133,46 +148,80 @@ public class HttpsUtils { |
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// 测试是否有效
|
|
|
// public static void main(String[] args) {
|
|
|
// // 接口地址
|
|
|
// String requestUrl = "http://localhost:8080/manifest/f5f5669bbdecefd3dacfaba194647c35";
|
|
|
// try {
|
|
|
//
|
|
|
// Map<String, Object> datas = new HashMap<>();
|
|
|
// datas.put("de_size", "dcdf38d9a5d6411985e49155481882f5");
|
|
|
// datas.put("waybillnomaster", "66611111111");
|
|
|
// datas.put("de_type", "001");
|
|
|
// datas.put("delivery_station", "001");
|
|
|
// datas.put("flightno", "CA9999");
|
|
|
// datas.put("flightdate", "2019-9-21 00:00:00");
|
|
|
// datas.put("originatingstation", "CGO");
|
|
|
// datas.put("destinationstation", "CGO");
|
|
|
// datas.put("totalpiece", "19");
|
|
|
// datas.put("totalweight", "19");
|
|
|
// datas.put("paymode", "PP");
|
|
|
// datas.put("customsstatus", "001");
|
|
|
// datas.put("customscode", "4604");
|
|
|
// datas.put("productname", "SHOUJI");
|
|
|
// datas.put("co_company", "测试");
|
|
|
// datas.put("co_address", "测试");
|
|
|
// datas.put("co_telephone", "00000000");
|
|
|
// datas.put("co_country", "CN");
|
|
|
// datas.put("sh_company", "测试");
|
|
|
// datas.put("sh_address", "测试");
|
|
|
// datas.put("sh_country", "CN");
|
|
|
// datas.put("sh_telephone", "0000000");
|
|
|
// datas.put("sh_city", "测试");
|
|
|
// datas.put("USER_ID", "1");
|
|
|
// datas.put("reach_station", "CGO");
|
|
|
// datas.put("de_volume", "000100003_MMM");
|
|
|
// datas.put("de_trstation", "APL,111");
|
|
|
//
|
|
|
// String param ="waybillnomaster=666-11111111&de_volume=66611111111_MMM&de_type=001&delivery_station=001&flightno=CV9765&flightdate=2019-12-20&originatingstation=CGO&destinationstation=ORD&totalpiece=1&totalweight=136.00&paymode=PP&customsstatus=001&customscode=4604&productname=CONSOL&co_company=DSV+AIR+%26+SEA+CO.%2c+LTD&co_address=8%269F%2c+RAFFLES+CITY+CHANGNING+OFFI.+TOWER+3%2c+NO.+1193+CHANGNING+RDSHANG&co_zipcode=200051&shpcusid=&shpaeo=&co_telephone=%2b86+21+33259800&co_country=CN&sh_company=ORD-DSV+AIR++SEA+INC&sh_address=1300+N+ARLINGTON+HEIGHTS+ROAD+SUITE+US&sh_zipcode=60143&cnecusid=&cneaeo=&sh_country=US&sh_telephone=8479568855&sh_fax=&sh_city=CHICAGO&USER_ID=1640&reach_station=ORD&de_trstation=BDX%2c111&de_packing=BDX%2c222&de_remarks=BDX%2c333&de_size=dcdf38d9a5d6411985e49155481882f5";
|
|
|
// String ss = HttpsUtils.sendPostHttpRequest(requestUrl, param);
|
|
|
// JSONArray jsonArray = JSON.parseArray(ss);
|
|
|
// } catch (Exception e) {
|
|
|
// e.printStackTrace();
|
|
|
// }
|
|
|
// }
|
|
|
/**
|
|
|
* 发送POST请求
|
|
|
*
|
|
|
* @param url
|
|
|
* 目的地址
|
|
|
* @param parameters
|
|
|
* 请求参数,Map类型。
|
|
|
* @return 远程响应结果
|
|
|
*/
|
|
|
public static String sendPost(String url, Map<String, Object> parameters) {
|
|
|
String result = "";// 返回的结果
|
|
|
BufferedReader in = null;// 读取响应输入流
|
|
|
PrintWriter out = null;
|
|
|
StringBuffer sb = new StringBuffer();// 处理请求参数
|
|
|
String params = "";// 编码之后的参数
|
|
|
try {
|
|
|
// 编码请求参数
|
|
|
if (parameters.size() == 1) {
|
|
|
for (String name : parameters.keySet()) {
|
|
|
sb.append(name).append("=").append(
|
|
|
java.net.URLEncoder.encode(parameters.get(name).toString(),
|
|
|
"UTF-8"));
|
|
|
}
|
|
|
params = sb.toString();
|
|
|
} else {
|
|
|
for (String name : parameters.keySet()) {
|
|
|
sb.append(name).append("=").append(
|
|
|
java.net.URLEncoder.encode(parameters.get(name).toString(),
|
|
|
"UTF-8")).append("&");
|
|
|
}
|
|
|
String temp_params = sb.toString();
|
|
|
params = temp_params.substring(0, temp_params.length() - 1);
|
|
|
}
|
|
|
// 创建URL对象
|
|
|
java.net.URL connURL = new java.net.URL(url);
|
|
|
// 打开URL连接
|
|
|
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
|
|
|
.openConnection();
|
|
|
// 设置通用属性
|
|
|
httpConn.setRequestProperty("Accept", "*/*");
|
|
|
httpConn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
httpConn.setRequestProperty("User-Agent",
|
|
|
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
|
|
|
// 设置POST方式
|
|
|
httpConn.setDoInput(true);
|
|
|
httpConn.setDoOutput(true);
|
|
|
// 获取HttpURLConnection对象对应的输出流
|
|
|
out = new PrintWriter(httpConn.getOutputStream());
|
|
|
// 发送请求参数
|
|
|
out.write(params);
|
|
|
// flush输出流的缓冲
|
|
|
out.flush();
|
|
|
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
|
|
|
in = new BufferedReader(new InputStreamReader(httpConn
|
|
|
.getInputStream(), "UTF-8"));
|
|
|
String line;
|
|
|
// 读取返回的内容
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
result += line;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|
|
|
out.close();
|
|
|
}
|
|
|
if (in != null) {
|
|
|
in.close();
|
|
|
}
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
} |
...
|
...
|
|