HTTPClient sends out two requests when using Basic Auth?

前端 未结 2 889
青春惊慌失措
青春惊慌失措 2020-12-31 07:14

I have been using HTTPClient version 4.1.2 to try to access a REST over HTTP API that requires Basic Authentication. Here is client code:

DefaultHttpClient          


        
相关标签:
2条回答
  • 2020-12-31 07:48

    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.

    0 讨论(0)
  • 2020-12-31 08:00

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

    0 讨论(0)
提交回复
热议问题