请求在 curl 中工作但不在 RestTemplate 中工作
Request working in curl but not in 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_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());
我得到一个 400 代码,所以它使 HttpClient 崩溃:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause
从 curl 语句到 RestTemplate 的等价物是什么?
有人有任何修复建议吗?
注意:我从阿尔卡特 6860 交换机向 Web 服务发出请求。
来自你的错误:
nested exception is
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
看起来您的请求正在处理,服务器正在响应 400 错误请求。 Spring 每当服务器 returns 4xx 错误代码时,RestTemplate 抛出 HttpClientErrorException
。
我找到了解决方案,无论如何感谢您的回复。
问题是 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 代码错误来回答。
为了修复它,我将符号“+”替换为 space,编码为“+”。
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());
我得到一个 400 代码,所以它使 HttpClient 崩溃:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause
从 curl 语句到 RestTemplate 的等价物是什么? 有人有任何修复建议吗?
注意:我从阿尔卡特 6860 交换机向 Web 服务发出请求。
来自你的错误:
nested exception is
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
看起来您的请求正在处理,服务器正在响应 400 错误请求。 Spring 每当服务器 returns 4xx 错误代码时,RestTemplate 抛出 HttpClientErrorException
。
我找到了解决方案,无论如何感谢您的回复。
问题是 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 代码错误来回答。
为了修复它,我将符号“+”替换为 space,编码为“+”。