发送post请求报错
报错: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);此处调不通