Android OkHttp with Basic Authentication

后端 未结 12 1537
感动是毒
感动是毒 2020-12-07 12:58

I\'m using the OkHttp library for a new project and am impressed with its ease of use. I now have a need to use Basic Authentication. Unfortunately, there is a dearth of w

12条回答
  •  [愿得一人]
    2020-12-07 13:47

    Here's the updated code:

    client.setAuthenticator(new Authenticator() {
      @Override
      public Request authenticate(Proxy proxy, Response response) throws IOException {
        String credential = Credentials.basic("scott", "tiger");
        return response.request().newBuilder().header("Authorization", credential).build();
      }
    
      @Override
      public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
        return null;
      }
    })
    

提交回复
热议问题