Unexpected end of file from server

对于这个问题网上有各种解决方案 , 我出现这个问题是因为两个原因。

  1. 使用了hutool的http工具包
  2. 请求返回的头的 Content-Type 不是 application/json 而是 text/plain;charset=UTF-8

出问题的代码如下 :

        HttpResponse response = HttpRequest.post(url)
                .timeout(5000)
                .body(bodyStr)
                .execute();

解决方案1

        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");
        HttpEntity<IssueCertParams> requestEntity = new HttpEntity<>(body, headers);

        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
                String resultStr = exchange.getBody();

使用这个方案restTemplate会可以处理 text/plain;charset=UTF-8 的响应。

解决方案2

        HttpResponse response = HttpRequest.post(url)
                .header(Header.ACCEPT,"text/plain;charset=UTF-8")
                .timeout(5000)
                .body(bodyStr)
                .execute();

在请求头里声明接收数据格式为 text/plain;charset=UTF-8

日志问题

排查问题需要一个好的日志框架,可以追踪日志,可以订阅日志,集成简单,可以使用

体验地址: http://119.163.197.219:13456/view/runtime/index.html#/log/aioLogPage

沟通加QQ群 : 908377977
gitee 开源地址 : https://gitee.com/aiocode/aio-runtime
github开源地址 : https://github.com/codeisangel/aio-runtime

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐