接口类 package org.common.serveradmin.conston;public class Constant { /***************************admincore接口url*****************/ //请求admincore的http前缀 woke-admincore-web public static String HTTP_ADMINCORE_URL_PREFEX="http
package org.common.serveradmin.conston;
public class Constant {
/***************************admincore接口url*****************/
//请求admincore的http前缀 woke-admincore-web
public static String HTTP_ADMINCORE_URL_PREFEX="http://localhost:8080/";
//退款
public static String HTTP_PAY_URL_PREFEX="http://192.168.0.39:8090/";
//拒绝退款
public static String HTTP_REJECTREASON_URL="refordOrder/refuseRefordOrder";
//同意退款
public static String HTTP_CONSENTREFUNDS_URL="refordOrder/applyRefordOrder";
/***************************admincore接口url********/
public static String RESPONSE_FAIL_DESP="响应失败";
public static String RESPONSE_FAIL_CODE="500";
public static String RESPONSE_SUCCESS_DESP="响应成功";
public static String RESPONSE_SUCCESS_CODE="200";
public static String TOKENCODE_FAIL_DESP="生成tokencode失败";
public static String TOKENCODE_FAIL_CODE="400";
public static String RESPONSE_AGAINLOGIN_DESP="TokenCode失效,请重新登录";
public static String RESPONSE_AGAINLOGIN_CODE="201";
}
pom.xml
httpClietnUtil实例4.3.6 4.3.6 3.1 org.apache.httpcomponents httpclient${httpclient.version} commons-httpclient commons-httpclient${commons-httpclient-version}
package org.woke.serveradmin.manager.utils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import org.common.serveradmin.conston.Constant;
import org.common.serveradmin.exception.ServerAdminException;
import org.woke.serveradfa.request.BaseRequestToOtherRequest;
import org.woke.serveradfa.response.BaseRespose;
import java.io.IOException;
import java.util.HashMap;
@Slf4j
public class HttpClientUtil {
private final static int SOCKET_TIMEOUT = 60000;//单位ms 从服务器读取数据的timeout
private final static int CONNECTION_TIMEOUT = 60000;//单位ms 和服务器建立连接的timeout
private final static int CONNECTION_REQUEST_TIMEOUT = 3000;//单位ms 从连接池获取连接的timeout
private RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
.build();
private static HttpClientUtil instance = null;
private HttpClientUtil() {
}
/**
* 单例获取httpClietnUtil实例
*
* @return httpClietnUtil实例
*/
public static HttpClientUtil getInstance() {
if (instance == null) {
instance = new HttpClientUtil();
}
return instance;
}
public String revokeTuniuMethod(String requestString, String url)
throws ServerAdminException,Exception {
BaseRespose baseRespose =new BaseRespose();
log.debug("传给的JSON:{}", requestString);
// 连接途牛接口,获取返回值
log.info("传给其他系统的参数(不包括data):{},请求地址:{}", requestString,url);
String charset = "UTF-8";
String contentType = "application/json";
String result = "";
try {
result = sendHttpPost(url,requestString,charset,contentType);
baseRespose=JSON.parseObject(result, BaseRespose.class);
if (baseRespose.getResponseCode().equals(Constant.RESPONSE_FAIL_CODE)){
throw new Exception();
}
if (!baseRespose.getResponseCode().equals(Constant.RESPONSE_SUCCESS_CODE)){
throw new ServerAdminException(baseRespose.getResponseCode(),baseRespose.getResoponseDisp());
}
} catch (IOException e) {
log.error("请求其他系统异常,请求参数url:{},参数信息:{},错误信息:{}",url,params,e);
e.printStackTrace();
}
return baseRespose.getReturnString();
}
/**
* 发送 post请求
* @param httpUrl 地址
* @param params 请求参数
* @param charset 字符编码
* @param contentType contentType跟请求参数要对应如:params是json根式,contentType为application/json
*/
public String sendHttpPost(String httpUrl, String params,String charset,String contentType)
throws IOException {
HttpPost httpPost = new HttpPost(httpUrl);
//设置参数
StringEntity stringEntity = new StringEntity(params, charset);
stringEntity.setContentType(contentType);
httpPost.setEntity(stringEntity);
return sendHttpPost(httpPost);
}
/**
* 发送Post请求
* @param httpPost
* @return
*/
private String sendHttpPost(HttpPost httpPost) throws IOException {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
// 创建默认的httpClient实例.
httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
// 执行请求
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return responseContent;
}
}
