报错:Failed to execute POST request
Map<String,Object> params = new HashMap<String, Object>();
params.put("systemName","root");
String param = JSONUtils.toJSON(params);
log.info(">>>调用接口param,JSON参数:{}", param);
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Connection", "keep-alive");
log.info(">>>headers,结果:{}", headers);
HttpUtils.HttpServiceResponse response = null;
String url = HostUrl;
log.info(">>>地址,结果:{}", url);
response = HttpUtils.post(url, headers, param);
log.info(">>>调用接口返回response.getResponseText(),结果:{}", response.getResponseText());
public static HttpServiceResponse post(String url, Map<String, String> headers, String text) {
CloseableHttpClient httpclient = createHttpClient(url);
// 设置连接超时时间、请求获取数据超时时间
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).build();
HttpPost postRequest = new HttpPost(url);
postRequest.setConfig(requestConfig);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
postRequest.setHeader(entry.getKey(), entry.getValue());
}
}
if (text != null && !text.isEmpty()) {
postRequest.setEntity(new StringEntity(text, "utf-8"));
}
logger.debug("execute POST request '{}' wiht headers '{}' and data: {}", url, headers, text);
try {
return getResponse(postRequest, httpclient.execute(postRequest));
} catch (IOException e) {
throw new HttpServiceException("Failed to execute POST request: " + url, e);
}
}
response = HttpUtils.post(url, headers, param);
现在是确定在这个位置错误
在这里执行不过去:Failed to execute POST request
暂时未找到原因
现在是发送json格式没问题。
调用接口: String url = HostUrl;时,能够postman发送成功,现在是本地运行代码,用本地地址127.0.0.1:8835时response = HttpUtils.post(url, headers, param);可以调通,能够调用此接口,但是在上环境后,response = HttpUtils.post(url, headers, param);此处调不通
可以参考以下几个方向进行定位:
1.请求的url所在服务器是不是指定访问的ip,如果是的话可以放开ip限制.
2.定位日志信息,打印一下具体的异常信息.
throw new HttpServiceException("Failed to execute POST request: " + url, e);
如有帮助,欢迎采纳!
本地测试可以,线上环境报错,试着排查一下部署环境的问题: