首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android basic auth okhttpclient

android basic auth okhttpclient
EN

Stack Overflow用户
提问于 2016-04-26 09:36:59
回答 1查看 899关注 0票数 2

我正在使用rest和okHttp客户机连接到rest。当我禁用Tomcat上的基本身份验证时,一切都完美无缺。当在Tomcat上启用Basic时,我会得到404 Page。这是我的身份验证和错误输出。

代码语言:javascript
复制
    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;
        }
    });

错误日志

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-03 14:35:55

使用RequestInterceptor解决问题。

代码语言:javascript
复制
  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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36860984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档