且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中使用HttpPost发送不记名令牌

更新时间:2023-02-24 12:38:13

授权"不应作为参数.它是标题.

The "Authorization" should not be a parameter. Its a header.

HttpPost request = new HttpPost(URL_SECURED_BY_BASIC_AUTHENTICATION);
String auth = DEFAULT_USER + ":" + DEFAULT_PASS;
byte[] encodedAuth = Base64.encodeBase64(
  auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);