gpt4 book ai didi

java - HttpClientErrorException getResponseBodyAsString() 无法将字节解析为字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:16 24 4
gpt4 key购买 nike

我在从第三方服务器的 403 http 代码响应获取响应正文时遇到问题。在 2019 年 4 月 5 日之前,我能够正确从第三方获取响应正文,但现在我正在检索一个可能无法从 getResponseBodyAsString 正确转换为字符串的字节。这是 org.springframework.web.client 包的问题吗?

    @Autowired
RestClient restClient;
try{
//Request via restTemplate
ResponseEntity<String> responseEntity = restClient.makeClientRequestJson(batch_type.getEndpoint(), HttpMethod.POST, httpHeaders, batch_payload.getRequest(), ProcessorConstants.moduleId);
} catch (HttpClientErrorException | HttpServerErrorException ex) {
loggingService.writeLogs("Error From Third Party Request, Message: " + ex.getMessage() + "| Response Body: " + ex.getResponseBodyAsString(), this.getClass(), LoggingEnum.ERROR, BatchConstants.moduleId);
...

这是 makeClientRequestJson 实现:

public ResponseEntity<String> makeClientRequestJson(String url, HttpMethod httpMethod, HttpHeaders headers, String jsonBody , String moduleId){
RestTemplate restTemplate = new RestTemplate();

HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
loggingService.writeLogs("Third Party Request: " + entity.getBody() + "| url: " + url + " | method: " + httpMethod.toString(), RestClientImpl.class, LoggingEnum.INFO, moduleId);

ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, entity, String.class);
loggingService.writeLogs("Third Party Response: " + response.getBody() + "| url: " + url + " | method: " + httpMethod.toString(), RestClientImpl.class, LoggingEnum.INFO, moduleId);
return response;
}

问题是当 ex.getResponseBodyAsString() 返回字节数组时,但我直接从服务器获取 json 响应,如下所示:

{
"code": "403020",
"transactionID": "1555495478-1409322457-83999786",
"message": "Account is Level 1"
}

这是我在调试代码时得到的示例值: Here is sample value I get when debugging the code

最佳答案

更新:这是由于客户端服务器的响应 header 造成的,其响应上有一些奇怪的 header ,例如连接 -> 关闭。如果有人遇到此问题,请务必先扫描 header 响应。

关于java - HttpClientErrorException getResponseBodyAsString() 无法将字节解析为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55725428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com