import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.goldpac.vtm.business.modules.pojo.GeneralApiParamInfo;
import com.goldpac.vtm.business.modules.pojo.GeneralApiResult;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
* Req_Mode_Soap_Post 请求模式
* @author wst(shi-tao.wen) 2021年9月13日 上午10:24:30
*
*/
public class ReqModeOkHttpPost {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public ReqModeOkHttpPost() {}
* SoapPost请求
* @author wst(shi-tao.wen) 2021年9月10日 下午1:47:51
* @param apiParam
* @return
*/
public GeneralApiResult OkHttpPost(GeneralApiParamInfo apiParam) {
logger.info("OkHttpPost --> ");
GeneralApiResult result = new GeneralApiResult();
try {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS).build();
String bodyJson = JSONObject.toJSONString(apiParam.getParams());
okhttp3.RequestBody body = okhttp3.RequestBody.create(MediaType.parse("application/json"), bodyJson);
Request request = new Request.Builder()
.url(apiParam.getActualServerUrl())
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful() || response.body() == null) {
result.setMsg("接口请求异常,返回信息为空");
result.setCode(500);
result.setData("接口请求异常,返回信息为空");
} else {
result.setMsg("接口请求成功");
result.setCode(200);
result.setData(response.body().string());
}
} catch (Exception e) {
result.setCode(500);
result.setData("");
result.setMsg("系统请求异常,请求超时或业务异常,请稍后再试" + (e.getMessage() == null ? "" : ("," +e.getMessage())));
logger.info(result.getMsg());
if (e.getCause() != null
&& e.getCause().toString().toLowerCase().contains("time")
&& e.getCause().toString().toLowerCase().contains("out")) {
}
e.printStackTrace();
}
return result;
}
}