使用RestTemplate调用Bing Search API时出现400 Bad-Request

时间:2016-01-21 10:20:47

标签: java rest restful-authentication resttemplate bing-api

使用RestTemplate向Bing Search API发送请求时,我一直收到400 BAD请求。我浏览了Bing Docs,但没有找到原因。

这是我的代码:

RestTemplate restTemplate = new RestTemplate();
String bingSearchText = "hello";
bingSearchText = bingSearchText.replaceAll(" ", "%20");

String accountKey="account-key";
byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
String accountKeyEnc = new String(accountKeyBytes);

String bingURL = new String("https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=%27" + bingSearchText + "%27&$top=50&$format=Atom");

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization", "Basic " + accountKeyEnc);

MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<String, String>();
multiValueMap.add("Authorization", "Basic " + accountKeyEnc);
HttpEntity<String> httpEntity = new HttpEntity<String>(multiValueMap);
try
{
    Map loginResponseAasMap = (Map) restTemplate.exchange(bingURL, HttpMethod.GET, httpEntity, Object.class);
}
catch(Exception e)
{
    e.printStackTrace();
}

0 个答案:

没有答案