2016-04-26 1 views
1

Я подключаюсь к моему отдыху api, используя модифицированный и okHttp-клиент. Когда я отключу Обычную проверку подлинности на Tomcat, все работает безупречно. Когда на Tomcat включен Basic Auth, я не могу найти страницу 404. Вот мой вывод аутентификации и ошибок.android basic auth okhttpclient

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

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

Журнал ошибок

D/YOUR_LOG_TAG: <--- HTTP 404 https://myserver:8443/RestWS/objects/barcodes (4462ms) 
    D/YOUR_LOG_TAG: Server: Apache-Coyote/1.1 
    D/YOUR_LOG_TAG: Cache-Control: private 
    D/YOUR_LOG_TAG: Expires: Thu, 01 Jan 1970 01:00:00 CET 
    D/YOUR_LOG_TAG: Set-Cookie: JSESSIONID=*********************; Path=/; Secure; HttpOnly 
    D/YOUR_LOG_TAG: Content-Type: text/html;charset=ISO-8859-1 
    D/YOUR_LOG_TAG: Content-Length: 127 
    D/YOUR_LOG_TAG: Date: Tue, 26 Apr 2016 09:31:22 GMT 
    D/YOUR_LOG_TAG: OkHttp-Selected-Protocol: http/1.1 
    D/YOUR_LOG_TAG: OkHttp-Sent-Millis: 1461663081883 
    D/YOUR_LOG_TAG: OkHttp-Received-Millis: 1461663081973 
    D/YOUR_LOG_TAG: <html> 
      D/YOUR_LOG_TAG: <head> 
      D/YOUR_LOG_TAG: <title>404-Page Not Found</title> 
      D/YOUR_LOG_TAG: </head> 
      D/YOUR_LOG_TAG: <body> The requested URL was not found on this server. </body> 
      D/YOUR_LOG_TAG: </html> 
      D/YOUR_LOG_TAG: <--- END HTTP (127-byte body) 
+1

Это показывает, что страница, которую вы ищете, отсутствует на стороне сервера. Вы должны проверить наличие бэкэнда. – GrIsHu

+0

Там я открываю его без проблем, когда я выписываю auth. Когда включено auth, я могу получить доступ к этой странице из браузера с Android. Я не могу – user2925656

+0

Для получения доступа потребуется определенное разрешение, поэтому вам придется авторизовать его всегда, прежде чем обращаться к нему в android. Вам необходимо передать учетные данные авторизации в заголовке. – GrIsHu

ответ

0

Проблема решена с помощью RequestInterceptor.

restAdapter = new RestAdapter.Builder() 
      .setConverter(new GsonConverter(gson)) 
      .setRequestInterceptor(new RequestInterceptor() { 
       @Override 
       public void intercept(RequestFacade request) { 
        String credentials = DownloadService.rest_user + ":" + DownloadService.rest_pw; 
        String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); 
        request.addHeader("Authorization", "Basic " + base64EncodedCredentials); 
       } 
      }) 
      .setEndpoint(DownloadService.URL) 
      .setClient(new OkClient(okHttpClient)) 
      .build();