使用授权标题发送时,Spring RestTemplate会发出401 Unauthorized错误,邮递员没有问题

时间:2018-05-08 18:52:04

标签: java rest postman

我们正试图通过Spring Rest Template访问Lithium Rest Api(它的https网站),如下所示

           `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);'

以上代码总是给出

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

通过邮递员调用相同的Web服务时没有问题,但是当通过java代码访问时,获得上述401未经授权的异常

0 个答案:

没有答案