Skip to main content

android webview with https connection and basic auth. How to get this working?


I have researched and researched and researched this until I've gone grey and bald. How on earth do I get a webview to work for a site that needs http basic authentication over an https connection on api level 8+



I have the following code




String email = Util.getEmail(this);
String pwd = Util.getPassword(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);

// webview.setWebViewClient(new MobileWebViewClient(this));
webview.loadUrl(url);



As you can see I did have a web view client (Now commented out) that overrides the onReceivedHttpAuthRequest method which looks like this




@Override
public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm){
String email = Util.getEmail(wvContext);
String pwd = Util.getPassword(wvContext);
if(!pwd.equalsIgnoreCase("-1") && !pwd.equalsIgnoreCase("-1")){
handler.proceed(email, pwd);
}
}



This was used without the webview.setHttpAuthUsernamePassword and works fine except that it means 2 requests are issued to the website - The first gets a 401 and then the client kicks in with the authorisation stuff This is fine for a small amount of website traffic but halving the amount of traffic (currently averaging 49 requests p/m) is the name of the game right now!



I read that I can pre-emptively supply the credentials by using




webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);



However this just results in Http Basic: Access denied errors The server base url constant is the domain name for the site i.e. https://example.com (without the page) the actual url is https://example.com/some_pages . It makes no difference whether I use the full url or the domain. I have checked the realm and I have that correct and I have used just empty strings providing only email and password. Could this be something to do with the fact that the site is using https? My code seems to work fine on my dev box without https but that may be a red herring.!



The only stack overflow questions that seem to cover my requirements have not got accepted answers and the docs are no help that I can see.



I now have such a large dent in my head from banging it against a brick wall that I am thinking of getting a square hat.



Please if anyone can give me the solution to this I will be forever in your debt! I might even e-Mail you an KitKat


Source: Tips4all
Source: Tips4allSource: CCNA FINAL EXAM

Comments

  1. It will work for https URL.In this if we are getting Untrusted_cer then we will ignore it

    webview.setWebViewClient(new WebViewClient(){
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
    HttpAuthHandler handler, String host, String realm) {
    super.onReceivedHttpAuthRequest(view, handler, host, realm);
    }

    @Override
    public void onReceivedSslError(WebView view,
    SslErrorHandler handler, SslError error) {
    super.onReceivedSslError(view, handler, error);
    if(error.getPrimaryError()==SslError.SSL_UNTRUSTED){
    handler.proceed();
    }else{
    handler.proceed();
    }
    }

    });


    I have no idea about second problem

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.