Spring RestTemplate gives 401 Unauthorized error when sending with Authorization Header, No Issues with postman

泪湿孤枕 提交于 2019-12-23 03:21:29

问题


We are trying to access the Lithium Rest Api (its a https site) through Spring Rest Template as below

           `String plainCreds = lswUserName + ":" + lswPassword;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    HttpEntity<String> request = new HttpEntity<String>(headers);
        ResponseEntity<String> sRawResp = restTemplate.exchange(completeURL.toString(), HttpMethod.GET, request, String.class);'

Above code always gives

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized Exception

There were no issues when invoking the same web service through postman, however when ever accessed through java code, getting the above 401 unauthorized exception

来源:https://stackoverflow.com/questions/50240474/spring-resttemplate-gives-401-unauthorized-error-when-sending-with-authorization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!