Spring 请求在curl中工作,但不在rest模板中工作

Spring 请求在curl中工作,但不在rest模板中工作,spring,curl,resttemplate,Spring,Curl,Resttemplate,以下curl调用正在工作,服务器响应为200代码: curl -i -H "Content-Type: application/json" -H "Cookie: wv_sess=emrri58a7f3qauof5ntfsc77k7" -X GET http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1 但当我尝试在Spring中使用以下代码对RestTemplate执行相同操作时: String cookies = "wv_s

以下curl调用正在工作,服务器响应为200代码:

curl -i -H "Content-Type: application/json" -H "Cookie: wv_sess=emrri58a7f3qauof5ntfsc77k7" -X GET http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1
但当我尝试在Spring中使用以下代码对RestTemplate执行相同操作时:

        String cookies = "wv_sess=emrri58a7f3qauof5ntfsc77k7";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Cookie",cookies);
        headers.set("Content-Type",MediaType.APPLICATION_JSON_VALUE);

        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange("http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1", HttpMethod.GET, entity, String.class);

        logger.info(response.toString());
从curl语句到rest模板的等价物是什么? 有人有什么建议来解决它吗

注意:我从阿尔卡特6860交换机向WebServices发出请求。

您的错误:

nested exception is 
org.springframework.web.client.HttpClientErrorException: 400 Bad Request

看起来您的请求正在通过,服务器正在响应400个错误请求。每当服务器返回4xx错误代码时,Spring RestTemplate就会抛出一个错误。

我找到了解决方案,感谢您的回复

问题是RestTemplate对我的URL进行了编码,我通过Wireshark检测到了它。

给定原始URL:

http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1
它被替换为“+”符号,结果如下:

http://192.168.1.1/cli/aos?cmd=show%2Binterfaces%2B1/1/1
因此,服务器当然检测到一个无效的URL,必须回答400代码错误


为了解决这个问题,我用符号“+”替换了一个编码为“+”的空间。那么,我在RestTemplate中做了什么不同的事情,使它无法工作呢?可能问题出在内容类型属性上,但我尝试了很多组合,但仍然不起作用。您是否尝试注销您正在发出的完整请求?执行如下所述的操作:。这将帮助您确定curl请求和RestTemplate请求之间的区别。服务器返回400错误请求状态的原因完全取决于服务器。。。你能在服务器上启用详细日志吗?我试过使用拦截器,但我只得到了对象引用。谢谢。这个给我修好了。我正在向Salesforce Rest API发送请求,它在RestTemplate中失败,在CURL中成功。我只需要在查询中用空格替换“+”。
http://192.168.1.1/cli/aos?cmd=show%2Binterfaces%2B1/1/1