Job Smith
Job Smith

Reputation: 263

Handling HTTP basic authentication using Android webview

I am using a webview to load a webpage which requires basic HTTP authentication. I have done some Google search and I knew it can be done by using a WebClientView and over-writing onReceivedHttpAuthRequest.

like this

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. 
        }
    });

what I have already tried is I use a Login activity to get the users's input and pass the parameter back to this webview activity.

But is this the best way of doing it?

Upvotes: 1

Views: 2184

Answers (1)

Satyam
Satyam

Reputation: 1682

try this method with HttPget and 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);

This is sample code for Authentication code in Httpget . You can try by this w't you want...

Upvotes: 1

Related Questions