public static String invoke(String jsondata, String url, String appsecret, String account, String version) throws Exception {
byte[] dataAssembleByte = RSAUtils.encryptByPublicKey(jsondata.getBytes("UTF-8"), appsecret);
String jsonDataBase64 = Base64.encodeBase64String(dataAssembleByte);
Map<String, Object> map = new HashMap<String, Object>();
map.put("method", METHOD);
map.put("version", version);
map.put("data", jsonDataBase64);
map.put("account", account);
map.put("timestamp", DateUtils.convertToString(new Date(), DateUtils.ISO_DATETIME_NO_T_FORMAT));
if (logger.isDebugEnabled()){
logger.debug("url=[" + url + "], " +
"method=[" + map.get("method") + "], " +
"version=[" + map.get("version") + "], " +
"jsondata=[" + jsondata + "], " +
"jsonDataBase64=["+ map.get("data") + "], " +
"timestamp=["+ map.get("timestamp") + "], " +
"account=[" + map.get("account") + "]");
}
long startTime = System.currentTimeMillis();
String result = HttpClientHelper.post(url, map);
long endTime = System.currentTimeMillis();
logger.info("调用OMS(新)系统接口耗时:" + (endTime - startTime) + "毫秒");
return result;
}
package com.yundaex.common.bidiboxing.helper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientHelper {
public static final String CHARSET_ENCODE = "UTF-8";
public static String post(String url, String data) throws Exception {
RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(30000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
HttpPost hp = new HttpPost(url);
HttpEntity responseEntity = null;
CloseableHttpResponse response = null;
int status = 0;
try {
StringEntity entity = new StringEntity(data, CHARSET_ENCODE);// 解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
hp.setEntity(entity);
response = httpClient.execute(hp);
status = (response == null ) ? 0 :response.getStatusLine().getStatusCode();
if (status == 200) {
responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, CHARSET_ENCODE);
return responseString;
} else {
hp.abort();
throw new YDInterfaceAccessException("响应编码为:" + status);
}
} finally {
if (null != responseEntity) {
EntityUtils.consumeQuietly(responseEntity);
}
if (null != response) {
try {
response.close();
} catch (IOException e) {
}
}
try {
httpClient.close();
} catch (IOException e) {
}
}
}
public String post(String url, Map<String, Object> data) throws Exception{
RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(30000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
HttpPost hp = new HttpPost(url);
HttpEntity responseEntity = null;
CloseableHttpResponse response = null;
int status = 0;
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
Object value = entry.getValue();
if(value != null){
pairs.add(new BasicNameValuePair(entry.getKey(), value.toString()));
}
}
try {
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(pairs, CHARSET_ENCODE);
hp.setEntity(uefEntity);
response = httpClient.execute(hp);
status = (response == null ) ? 0 :response.getStatusLine().getStatusCode();
if (status == 200) {
responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, CHARSET_ENCODE);
return responseString;
} else {
hp.abort();
throw new YDInterfaceAccessException("响应编码为:" + status);
}
} finally {
if (null != responseEntity) {
EntityUtils.consumeQuietly(responseEntity);
}
if (null != response) {
try {
response.close();
} catch (IOException e) {
}
}
try {
httpClient.close();
} catch (IOException e) {
}
}
}
}
public class HttpClientHelperTest {
public static void main(String[] args) {
String url = "http://10.19.105.184:7001/wms/systemConfig/query.do";
String data =
"{"+
" \"data\":{ "+
" \"criterias\":{ "+
" \"sysKey\":\"IFM_SWITCH\" "+
" } "+
" }, "+
" \"username\":\"wms_test\", "+
" \"format\":\"json\", "+
" \"dateTime\":\"2021426925\", "+
" \"validation\":\"bfbb29498ae850d16d4bea5eb3469048\" "+
" }"
;
String responseString ="";
try {
responseString = HttpClientHelper.post(url, data);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(responseString);
}
}