【发布时间】:2015-03-13 12:24:32
【问题描述】:
我可以使用邮递员执行以下 http 请求: 如您所见:
{
"succes":false
}
现在我需要使用 restTemplate 执行相同的请求。
为了实现它,我编写了以下代码:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("secret", "6Le8YwMTAAAAADTIDNBvjxg-x83jt5QvPN-dFGWs");
map.add("response", recapchaResponse);
HttpHeaders headers = new HttpHeaders();
HttpEntity<?> entity = new HttpEntity<Map>(map, headers);
restTemplate.exchange("https://www.google.com/recaptcha/api/siteverify", HttpMethod.POST, entity,ReCaptchaResponse.class);
当我在调试中执行以下代码时,我看到以下响应:
如您所见,响应包含错误。看起来response 和secret 没有被服务器接收。为什么?
我做错了什么?
附言
我已经使用 httpClient 编写了模拟
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("https://www.google.com/recaptcha/api/siteverify");
postMethod.setParameter("secret", "6Le8YwMTAAAAADTIDNBvjxg-x83jt5QvPN-dFGWs");
postMethod.setParameter("response", recapchaResponse);
client.executeMethod(postMethod);
String responseFromServer = new String(postMethod.getResponseBody());
效果很好。
【问题讨论】:
标签: spring httprequest recaptcha multipartform-data resttemplate