2015-08-31 47 views

回答

0

隨着OkHttp你可以在你OkHttpClient創建Authenticator像這樣這樣

OkHttpClient myOkHttpClient = new OkHttpClient(); 
myOkHttpClient.setAuthenticator(new Authenticator() { 
     @Override 
     public Request authenticate(Proxy proxy, Response response) throws IOException { 
      String credential = Credentials.basic("Username", "Password"); 
      return response.request().newBuilder().header("Authorization", credential).build(); 
     } 

     @Override 
     public Request authenticateProxy(Proxy proxy, Response response) throws IOException { 
      return null; 
     } 
    }); 

這應該做的工作。我發現使用基本身份驗證的文檔並不多,但我找到了一些幫助:Android OkHttp with Basic Authentication

+0

方法身份驗證在服務器返回401錯誤時有效。最好的方法是在攔截器的Header中設置證書。 – qpator