【发布时间】:2017-09-05 06:57:02
【问题描述】:
我遇到过类似的问题,但他们似乎没有提供直接的例子。我正在尝试使用 RestTemplate 通过编码的 url 获取。我有一个亚马逊类助手来生成签名的 url,但返回编码的 url,所以当我在 RestTemplate 中使用它时,它会再次编码。我应该输入 RestTemplate 的 url 应该是这样的;
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Keywords=php&Operation=ItemSearch&ResponseGroup=Images,ItemAttributes,Offers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-09-05T06:47:25.703Z&Signature=dthAE5BwmK2aZmSoIPRBwsPgCNwIv6JnXoqjC0QyRCQ=
但是我的亚马逊助手给了我这个;
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Keywords=php&Operation=ItemSearch&ResponseGroup=Images%2CItemAttributes%2COffers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-09-05T06%3A47%3A25.703Z&Signature=dthAE5BwmK2aZmSoIPRBwsPgCNwIv6JnXoqjC0QyRCQ%3D
再次编码打破了我的时间戳。我知道我可以很容易地使用字符串替换,但我正在寻找一种更好的方法。
我的代码是这样的;
SignedRequestsHelper 助手;
try {
helper = SignedRequestsHelper.getInstance(this.ENDPOINT, this.ACCESS_KEY_ID, this.SECRET_KEY);
} catch (Exception e) {
return "ERROR";
//e.printStackTrace();
}
String requestUrl = null;
Map<String, String> params = new HashMap<String, String>();
...
requestUrl = helper.sign(params);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ResponseErrorHandler());
...
try {
String response = restTemplate.getForObject(requestUrl, String.class);
} catch (HttpStatusCodeException exception) {
return exception.getStatusCode().toString();
}
【问题讨论】:
标签: java spring-mvc spring-boot resttemplate