HTTPClient sends out two requests when using Basic Auth?

蹲街弑〆低调 提交于 2019-11-30 08:41:10

Rather than using .setCredentials() why don't you just encode USERNAME:PASSWORD and add the authentication header with .addHeader()

This means that your server/target endpoint is creating a new session for every client request. This forces every request of yours to go through a hand-shake, which means the clients first makes the call and realizes that it needs authorization, then it follows with the authorization. What you need to do is send the authorization preemptively as follows:

httpClient.getParams().setAuthenticationPreemptive(true);

Just to understand the process you may log your client request headers, to give you an idea of what your client is sending and receiving: See if this works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!