Java 400使用RestTemplate调用Bing搜索API时请求错误
在使用RestTemplate向Bing搜索API发送请求时,我一直收到400个错误请求。我查看了Bing文档,但没有找到原因 这是我的密码:Java 400使用RestTemplate调用Bing搜索API时请求错误,java,rest,restful-authentication,resttemplate,bing-api,Java,Rest,Restful Authentication,Resttemplate,Bing Api,在使用RestTemplate向Bing搜索API发送请求时,我一直收到400个错误请求。我查看了Bing文档,但没有找到原因 这是我的密码: RestTemplate restTemplate = new RestTemplate(); String bingSearchText = "hello"; bingSearchText = bingSearchText.replaceAll(" ", "%20"); String accountKey="account-key"; byte[]
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();
}
RestTemplate RestTemplate=new RestTemplate();
字符串bingSearchText=“hello”;
bingSearchText=bingSearchText.replaceAll(“,“%20”);
字符串accountKey=“accountKey”;
字节[]accountKeyBytes=Base64.encodeBase64((accountKey+“:“+accountKey).getBytes());
String accountKeyEnc=新字符串(accountKeyBytes);
字符串bingURL=新字符串(“https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=%27“+bingSearchText+%27&$top=50&$format=Atom”);
HttpHeaders HttpHeaders=新的HttpHeaders();
添加(“授权”、“基本”+accountKeyEnc);
MultiValueMap MultiValueMap=新链接的MultiValueMap();
多值映射。添加(“授权”、“基本”+accountKeyEnc);
HttpEntity HttpEntity=新的HttpEntity(多值映射);
尝试
{
映射loginResponseAsMap=(映射)restTemplate.exchange(bingURL、HttpMethod.GET、httpEntity、Object.class);
}
捕获(例外e)
{
e、 printStackTrace();
}
您是否有办法记录正在进行的实际通话?我很想知道你的“%27”是否又被编码了。为什么我没有想到这一点呢@非常感谢。