1 回答
TA贡献1868条经验 获得超4个赞
鉴于您尝试使用基本身份验证,这应该足够了
val credentialsProvider = new BasicCredentialsProvider()
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("username", "password")
)
val httpClient =
HttpClientBuilder.create()
.setDefaultCredentialsProvider(credentialsProvider)
.build()
val httpResponse = new HttpGet(url)
httpClient.execute(httpResponse)
相反,如果您更喜欢使用简单的HTTP标头,则可以使用
def buildEncodedCredentials(): String = {
val credentialsString = username + ":" + password
val charset = StandardCharsets.ISO_8859_1
val encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(charset))
return new String(encodedBytes, charset)
}
httpResponse.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + buildEncodedCredentials())
添加回答
举报