我使用下面通過RestTemplate在春季4檢索JSON:春RestTemplate收到「401未授權」
protected DocInfoResponse retrieveData(String urlWithAuth) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + auth.getSig());
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<DocInfoResponse> response = restTemplate.exchange(urlWithAuth, HttpMethod.GET, request, DocInfoResponse.class);
return response.getBody();
}
我使用相同的代碼(具有不同響應等級)成功地得到了一個JSON文檔同一網站(使用不同的參數獲取不同的文檔)。
當我執行上面的代碼,我收到下面的堆棧跟蹤(部分):
Caused by: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
任何人都可以點我,爲什麼這可能是接收例外呢?
您是否嘗試過從瀏覽器或郵遞員訪問相同內容?它是否使用此身份驗證在那裏工作? – Coder
是的,我已經獲得了相同的URL以成功返回瀏覽器中的預期結果。 – Squigglylot
它肯定是一個身份驗證問題。您是否生成用於身份驗證的數字簽名?簽名URL是特定的嗎? – Coder