B4J Question (solved) Webview Username & Password

sdleidel

Active Member
Licensed User
Longtime User
Hi, I would like to access a website that is
protected by username & password(htacceess & htpasswd)
via Webview.

Is there a solution for this ?

Thanks
Sascha
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

swChef

Active Member
Licensed User
Longtime User
Based upon this page, I found inserting the some additional UserAgent content (between lines 1 and 2 above) gives better support across applications tested so far. The SAP web pages wouldn't display anything, except for the login page, without it. I'm not sure it is the ideal setUserAgent content yet for current javaFX WebView; that page was written in 2014. At least it's a step forward.

B4X:
SetUserAgent(WebView1, $"foo
Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv)
AppleWebKit/537.36 (KHTML, like Gecko)
Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36
Authorization: Basic YourBase64EncodedCredentials"$) 'make sure that there is no space at the beginning of this line

I then found another UserAgent string in the SetUserAgent post link Erel provided above in his post #2.

There is also a getUserAgent in the WV engine; while checking that JavaFX WebView on W10 returned (on my Pro machine)
B4X:
Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/606.1 (KHTML, like Gecko)
JavaFX/11 Safari/606.1

Of course we can use getUserAgent and combine with the setUserAgent, and also shown here is the Base64 encoding.

B4X:
Sub GetUserAgent As String
    Return joWVeng.RunMethod("getUserAgent",Null)
End Sub
Sub SetUserAgent(UserAgent As String)
    Log("SetUserAgent, before " & GetUserAgent)
    joWVeng.RunMethod("setUserAgent",Array As String(UserAgent))
    Log("SetUserAgent, after  " & GetUserAgent)
End Sub

and somewhere in your code
    Dim s64input As String = User&":"&PW
    Dim Su As StringUtils
    Dim data() As Byte = s64input.GetBytes("UTF8")
    Dim s64output As String = Su.EncodeBase64(data)
    SetUserAgent($"foo
${GetUserAgent}
Authorization: Basic "$ & s64output) 'make sure that there is no space at the beginning of this line

Apologies for the mixed $ and non-$ strings.
And despite the above, I didn't manage to get all SAP Tiles' (opened) forms pages to display, but at least now some do.
 
Last edited:
Upvote 0
Top