Android Question Problem with Https and WebView

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

I'm having a problem with a page using https and webview. When accessing the site :

https://sitesample.com:9001/test/test.html (hypothetical example) - with smartphone browser, runs ok

Using the same site in a webview, I get a blank screen. I think that that the cause could be the https certificate (running on mobile browser but with some incompatibility with webview), but I'm not sure. How can I debug this to know what is happening? If the problem is the certificate, is there any way to do a workaround without needing to change it (for example, some security line in manifest...)?

Thanks!
 

Semen Matusovskiy

Well-Known Member
Licensed User
I also am not sure that blank screen always means wrong SSL certicate. Did you try to download a page using OkHttp ? If SSL is wrong, should be clear error message in log (as I remember).

If you want to process SSL error in WebView you need to process onReceivedSslError event in WebViewClient according Google Play rules. These rules talk that you should not allow all certificates, because this is unsafe.

Theoretically it's possible to decompile latest WebViewWrapper in Core.jar and to add some lines (if B4A stuff will allow).
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
@Marcos Alves

A possible solution to your problem could be:

1. Download this library https://www.b4x.com/android/forum/threads/webviewextras.12453/

2. Use this code to check for an SSL certificate error: (Thanks to @warwound and @Erel for the "Original" code)
Modify as you require - guideline only

B4X:
Sub Globals
    Private WebView1 As WebView
    Dim WebViewExtras1 As WebViewExtras
    Dim WebViewSetting1 As WebViewSettings
End Sub

Sub Activity_Create(FirstTime As Boolean)
    WebViewExtras1.Initialize(WebView1)
    Dim WebViewClient1 As DefaultWebViewClient
    WebViewClient1.Initialize("WebViewClient1")
    
    WebViewExtras1.SetWebViewClient(WebViewClient1)
    
    WebViewExtras1.LoadUrl("https://sitesample.com:9001/test/test.html")
End Sub


Sub WebViewClient1_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
    Log("WebViewClient1_ReceivedSslError: "&SslError1.GetUrl)
    
    '    you might want to take action depending on the type of the SSL error:
    Select SslError1.GetPrimaryError
        Case SslError1.SSL_DATE_INVALID
            Log("SSL_DATE_INVALID")
            
        Case SslError1.SSL_EXPIRED
            Log("SSL_EXPIRED")
            
        Case SslError1.SSL_IDMISMATCH
            Log("SSL_IDMISMATCH")
            
        Case SslError1.SSL_INVALID
            Log("SSL_INVALID")
            
        Case SslError1.SSL_MAX_ERROR
            Log("SSL_MAX_ERROR")
            
        Case SslError1.SSL_NOTYETVALID
            Log("SSL_NOTYETVALID")
            
        Case SslError1.SSL_UNTRUSTED
            Log("SSL_UNTRUSTED")
            
    End Select

    LogColor("SSL Error: "&SslError1.GetUrl,Colors.Red)

    '    you might want to compare the url that raised the error with a known trusted URL and Proceed or Cancel with the page loading:
    If SslError1.GetUrl = "https://sitesample.com:9001/test/test.html" Then
        Log("Proceed with the SSL Error")
        btnCancel2.SetVisibleAnimated(600,False)
        btnApprove.SetVisibleAnimated(900,False)
        
        SslErrorHandler1.Proceed
    Else
        SslErrorHandler1.Cancel
    End If
    
End Sub

Now, once you have established what the problem could be - you can take the next steps.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
A possible solution to your problem could be
Thank you for posting. I should mark your post as bookmark. This question came out a few times. And always i did not remember where the code is :D
 
Last edited:
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
I should mark your post as bookmark.

It will be an honour to be "Book Marked" by you Manfred.

Enjoy your day.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
@Marcos Alves

A possible solution to your problem could be:

1. Download this library https://www.b4x.com/android/forum/threads/webviewextras.12453/

2. Use this code to check for an SSL certificate error: (Thanks to @warwound and @Erel for the "Original" code)
Modify as you require - guideline only

B4X:
Sub Globals
    Private WebView1 As WebView
    Dim WebViewExtras1 As WebViewExtras
    Dim WebViewSetting1 As WebViewSettings
End Sub

Sub Activity_Create(FirstTime As Boolean)
    WebViewExtras1.Initialize(WebView1)
    Dim WebViewClient1 As DefaultWebViewClient
    WebViewClient1.Initialize("WebViewClient1")
   
    WebViewExtras1.SetWebViewClient(WebViewClient1)
   
    WebViewExtras1.LoadUrl("https://sitesample.com:9001/test/test.html")
End Sub


Sub WebViewClient1_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
    Log("WebViewClient1_ReceivedSslError: "&SslError1.GetUrl)
   
    '    you might want to take action depending on the type of the SSL error:
    Select SslError1.GetPrimaryError
        Case SslError1.SSL_DATE_INVALID
            Log("SSL_DATE_INVALID")
           
        Case SslError1.SSL_EXPIRED
            Log("SSL_EXPIRED")
           
        Case SslError1.SSL_IDMISMATCH
            Log("SSL_IDMISMATCH")
           
        Case SslError1.SSL_INVALID
            Log("SSL_INVALID")
           
        Case SslError1.SSL_MAX_ERROR
            Log("SSL_MAX_ERROR")
           
        Case SslError1.SSL_NOTYETVALID
            Log("SSL_NOTYETVALID")
           
        Case SslError1.SSL_UNTRUSTED
            Log("SSL_UNTRUSTED")
           
    End Select

    LogColor("SSL Error: "&SslError1.GetUrl,Colors.Red)

    '    you might want to compare the url that raised the error with a known trusted URL and Proceed or Cancel with the page loading:
    If SslError1.GetUrl = "https://sitesample.com:9001/test/test.html" Then
        Log("Proceed with the SSL Error")
        btnCancel2.SetVisibleAnimated(600,False)
        btnApprove.SetVisibleAnimated(900,False)
       
        SslErrorHandler1.Proceed
    Else
        SslErrorHandler1.Cancel
    End If
   
End Sub

Now, once you have established what the problem could be - you can take the next steps.
Thanks @BillMeyer ! I'll test. I'm sure that the problem is the certificate because I tested other pages in our servers (some without ssl and other with a distinct certificate provider), and worked.
I think that the problem is the GoGetSSL Certificate. I started to use this provider because the prices... in gogetssl a Https certificate costs US$ 8 (2 years, 99,99% effective - It looks that I found the 0,01% )! In GoDaddy the same certificate starts at US$ 150!
I tested other pages in our server with godaddy SSl certificate and works. Then, if I can make this work with gogetssl, I'll save at least US$ 142/site! A detail: I tested GoGet SSl pages with Android default browser or even chrome (outside webview) and worked fine.
I'll test and post results here... thanks again!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In my Webhosting i can easily add a SSL-Certificate using letsencrypt. Just needs a few clicks here.

But also for my VPS (VPS at Strato) it was easy to add a letsencrypt-Certificate using their linux tools
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
@Marcos Alves

A possible solution to your problem could be:

1. Download this library https://www.b4x.com/android/forum/threads/webviewextras.12453/

2. Use this code to check for an SSL certificate error: (Thanks to @warwound and @Erel for the "Original" code)
Modify as you require - guideline only

B4X:
Sub Globals
    Private WebView1 As WebView
    Dim WebViewExtras1 As WebViewExtras
    Dim WebViewSetting1 As WebViewSettings
End Sub

Sub Activity_Create(FirstTime As Boolean)
    WebViewExtras1.Initialize(WebView1)
    Dim WebViewClient1 As DefaultWebViewClient
    WebViewClient1.Initialize("WebViewClient1")
   
    WebViewExtras1.SetWebViewClient(WebViewClient1)
   
    WebViewExtras1.LoadUrl("https://sitesample.com:9001/test/test.html")
End Sub


Sub WebViewClient1_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
    Log("WebViewClient1_ReceivedSslError: "&SslError1.GetUrl)
   
    '    you might want to take action depending on the type of the SSL error:
    Select SslError1.GetPrimaryError
        Case SslError1.SSL_DATE_INVALID
            Log("SSL_DATE_INVALID")
           
        Case SslError1.SSL_EXPIRED
            Log("SSL_EXPIRED")
           
        Case SslError1.SSL_IDMISMATCH
            Log("SSL_IDMISMATCH")
           
        Case SslError1.SSL_INVALID
            Log("SSL_INVALID")
           
        Case SslError1.SSL_MAX_ERROR
            Log("SSL_MAX_ERROR")
           
        Case SslError1.SSL_NOTYETVALID
            Log("SSL_NOTYETVALID")
           
        Case SslError1.SSL_UNTRUSTED
            Log("SSL_UNTRUSTED")
           
    End Select

    LogColor("SSL Error: "&SslError1.GetUrl,Colors.Red)

    '    you might want to compare the url that raised the error with a known trusted URL and Proceed or Cancel with the page loading:
    If SslError1.GetUrl = "https://sitesample.com:9001/test/test.html" Then
        Log("Proceed with the SSL Error")
        btnCancel2.SetVisibleAnimated(600,False)
        btnApprove.SetVisibleAnimated(900,False)
       
        SslErrorHandler1.Proceed
    Else
        SslErrorHandler1.Cancel
    End If
   
End Sub

Now, once you have established what the problem could be - you can take the next steps.
Thanks @BillMeyer . With the code I could show the page in webview , after capturing and bypassing the SSL_UNTRUSTED error! Now I have other problem:

I'm trying to run a site similar to this: https://rtcmulticonnection.herokuapp.com/demos/Video-Conferencing.html
This site runs over a node.js and opens a mesh videoconf in browser (works very well in mobile navigators...) . When trying to open in a webview, the buttons don't work and WebRtc element doesn't start, besides, like I told, works very well in the smartphone navigator.
I thought that WebView should be similar to a embedded browser in the app but it looks that it isn't... any suggestion ?
 
Upvote 0
Top