使用Android webview处理HTTP基本身份验证

时间:2012-12-18 02:43:57

标签: android http

我使用webview加载需要基本HTTP身份验证的网页。我已经完成了一些Google搜索,我知道可以使用WebClientView并覆盖onReceivedHttpAuthRequest来完成。

像这样

webView.setWebViewClient(new WebViewClient() {

        public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm)        {
            // TODO Auto-generated method stub

      handler.proceed(username, password); //question is here,let user input these. 
        }
    });

我已经尝试过的是使用Login活动来获取用户的输入并将参数传递回此webview活动。

但这是最好的方法吗?

1 个答案:

答案 0 :(得分:1)

使用HttPget和HttpPost尝试此方法

        CredentialsProvider credProvider = new BasicCredentialsProvider();

        credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, 

        AuthScope.ANY_PORT,AuthScope.ANY_REALM),  new UsernamePasswordCredentials("admin", 
       "guest"));

        httpclient.setCredentialsProvider(credProvider);
        Log.w("SENCIDE", "Execute HTTP Get Request");
        HttpResponse response = httpclient.execute(httpget);

这是Httpget中验证代码的示例代码。你可以试试这个你想要的......