HttpClientBuilder basic auth

后端 未结 4 1591
旧巷少年郎
旧巷少年郎 2020-12-03 05:39

Since HttpClient 4.3, I have been using the HttpClientBuilder. I am connecting to a REST service that has basic authentication. I am setting the credentials as follows:

4条回答
  •  温柔的废话
    2020-12-03 05:59

    Actually, since you already trust the server, it's probably easiest to just construct the authorization header yourself.

     byte[] credentials = Base64.encodeBase64((username + ":" + password).getBytes(StandardCharsets.UTF_8));
     request.setHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
     httpClient.execute(request);
    

    This is just one of those cases were it's easier to read the spec, and roll it yourself.

提交回复
热议问题