【发布时间】:2016-11-30 19:35:21
【问题描述】:
我听说在到达包含密码/敏感信息的链接时,最喜欢使用 Post。
Q1.我知道对于名称-值对参数,Post 比 Put 更好,Get 将它暴露在 URL 中,而 Post 没有。但是,如果我们谈论的是身份验证,那么当我将凭据设置为 HttpClient 而不是 HttpGet 或 HttpPost 时,我使用什么方法有什么关系。所以无论如何凭据都不会暴露。名称-值对的封装通过使用受益发布。
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("user", "passwd"));
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
第二季度。同样,我可以将凭据设置为 httppost 的标头,也可以像这样设置为 httpget ..
String encoding = new BASE64Encoder().encode("user:passwd".getBytes());
httpGet.setHeader("Authorization", "Basic " + encoding);
httpPost.setHeader("Authorization", "Basic " + encoding);
post 方法在哪里优先于 get 方法?
【问题讨论】:
标签: java authentication post get httpclient