edi233
edi233

Reputation: 3031

Wrong show page with authentication in webview in android

I have a webView where url is: https://uesr:[email protected]:443/test-mobile and when I want to load this page it is unaviable, where I delete authentication from page and delete user:user@ page work. How add authentication to webview to work with that url?

this is my webView:

formWebView.getSettings().setJavaScriptEnabled(true);
formWebView.loadUrl(url);
formWebView.getSettings().setBuiltInZoomControls(true);

Upvotes: 0

Views: 947

Answers (2)

nilesh patel
nilesh patel

Reputation: 832

Write below code in your onCreate method.

webView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient 
{   

    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {

        handler.proceed("UserName", "Password");
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if (dialog != null) {
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }
    }

    public void onLoadResource(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onLoadResource(view, url);

    }

    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        // TODO Auto-generated method stub
        super.onScaleChanged(view, oldScale, newScale);
    }

    public void onTooManyRedirects(WebView view, Message cancelMsg,
            Message continueMsg) {
        // TODO Auto-generated method stub
        super.onTooManyRedirects(view, cancelMsg, continueMsg);
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub




        return super.shouldOverrideUrlLoading(view, url);

    }

    public void doUpdateVisitedHistory(WebView view, String url,
            boolean isReload) {
        // TODO Auto-generated method stub
        super.doUpdateVisitedHistory(view, url, isReload);

    }

    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        // TODO Auto-generated method stub
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    public void onPageFinished(WebView view, String url) {
        if (dialog != null) {

            dialog.dismiss();
        }
    }

}

Upvotes: 0

edi233
edi233

Reputation: 3031

I resolve my problem by:

    formWebView.setWebViewClient(new WebViewClient() {
          @Override 
          public void onReceivedHttpAuthRequest(WebView view,
                                                HttpAuthHandler handler,
                                                String host,
                                                String realm){ 
            handler.proceed(loggedUser.getLogin(), loggedUser.getPass());
          } 

          public void onReceivedSslError(WebView view,
                                         SslErrorHandler handler,
                                         SslError error) {
            handler.proceed() ;
          }
        });

Upvotes: 2

Related Questions