唐曌 2022-07-20 11:13 采纳率: 90%
浏览 46
已结题

post请求不通过,json格式正确,问题已定位

发送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);此处调不通

  • 写回答

2条回答 默认 最新

  • 卖柴火的小伙子 2022-07-20 13:31
    关注

    可以参考以下几个方向进行定位:
    1.请求的url所在服务器是不是指定访问的ip,如果是的话可以放开ip限制.
    2.定位日志信息,打印一下具体的异常信息.

    throw new HttpServiceException("Failed to execute POST request: " + url, e);
    

    如有帮助,欢迎采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月21日
  • 已采纳回答 7月21日
  • 修改了问题 7月20日
  • 创建了问题 7月20日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度