Android Question Webview with POSTData and additional headers

Danie Steyn

Member
Licensed User
Longtime User
Good morning. I need a bit of help, hoping someone out there can assist.
I need to connect to a website that requires me to login by sending POSTData with usernames and passwords, this then returns JSON data that I need to process.
I've tried WebWiewExtras, but cannot figure out how to get this done. Something else I've tried is doing the request using a httpjob and displaying the result in a webview but I cannot seem to get it.
Below is what I do in Visual Basic.
Login:
Dim PostString As String
PostString = "{ ""ClientType"": ""LEGEND"",""Username"": """ & username & """,""Password"": """ & password & """,""OperatorName"": """ & "JOHN" & """,""isOperatorAdmin"": " & True & ",""TillName"": """ & "POS1" & """,""PrinterWidth"": """ & 40 & """ }"
Dim urlstring As String = "http://127.0.0.1:8080/DesertView/LoginServlet"
Dim additionalHeaders As String = "Content-Type: application/json"
Dim targetFrameName As String = ""
WebBrowser1.Navigate(urlstring, targetFrameName, StringToBytes(PostString), additionalHeaders)
WebBrowser1.Refresh(WebBrowserRefreshOption.Completely)

Response:
    Public Function DesertViewResponse(ByVal jsonResponse As String) As Boolean
        'The response messages from DesertView will be caught here
        'Responses are json Encoded.
        

        'Deserialize json response into object (i'm using NewtonSoft but anything will work)
        Dim objResponse = JsonConvert.DeserializeObject(Of Legend_VoucherResponse)(jsonResponse)
            

        'Deserialize the Print String
        Dim bytes() As Byte
        Dim base64Decode As String
        bytes = System.Convert.FromBase64String(objResponse.PrintString)
        base64Decode = System.Text.UTF8Encoding.UTF8.GetString(bytes)
        


        RetValue.MessageType = objResponse.MessageType
        RetValue.PrintString = base64Decode
        RetValue.Cost = objResponse.Cost
        RetValue.HasVat = objResponse.HasVat
        RetValue.Value = objResponse.Value
        RetValue.VariableRate = objResponse.VariableRate
        RetValue.VoucherCode = objResponse.VoucherCode
        RetValue.VoucherName = objResponse.VoucherName

        
    End Function

    
End Class
 

drgottjr

Expert
Licensed User
Longtime User
setting aside the json issue for a minute, exactly how did you implement the httpjob? was there an answer from the server? exactly what was it (show log entry). httpjob is the way to deal with what you're looking for. postdata works.
 
Upvote 0

Danie Steyn

Member
Licensed User
Longtime User
setting aside the json issue for a minute, exactly how did you implement the httpjob? was there an answer from the server? exactly what was it (show log entry). httpjob is the way to deal with what you're looking for. postdata works.
I've tried using
JavaObject:
Private jo As JavaObject = WebView1
    Try
        jo.RunMethod("postUrl",Array As Object(tempPostUrl,bytesOfPOstRequest))
    Catch
        MsgboxAsync (LastException.Message,"")
    End Try
with this I get "not Logged in" in my Webview

With this
httppost:
Job1.PostBytes(tempPostUrl,bytesOfPOstRequest)
    Job1.GetRequest.SetHeader("Content-Type","application/json")
    
    Wait For (Job1) JobDone(job As HttpJob)
    If Job1.Success = True Then
        
        MsgboxAsync(job.GetString,"")
        WebView1.LoadHtml(job.GetString)
    Else
        
    End If
I just get version number from the server, no images
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
first you say "this then returns JSON data that I need to process".
now you talk about images ??? you're expecting images as json data??

if you are trying to mimic a webpage login that returns json data,
forget webview. you want httpjob.

if you are, in fact, logging into a webpage which is expecting posted
login credentials and which then displays images upon successful
login, then you want webview.

technically, it is certainly possible to download an html stream by posting
login credentials and then passing that stream to a webview, but if the
end result is to display images in a webview, what is the point of
accessing the resource with httpjob? just load the url in a webview with
posted login credentials and look at the images.

is bytesOfPOstRequest in the correct format expected by the server?
if the server is yours, you need to echo what was received and what was
expected. if the server isn't yours, you need an echoing test server. there
are some out there. you need to know why you're not logged in.

in your httppost snippet, you don't actually send the request. is this
actual code from your project? or are you paraphrasing?
also, assuming sending the request were actually there, where would
you put it? (before or after setheader?) also, your setheader request
is wrong. content-type has its own version: job1.GetRequest.SetContentType...

in your javaobject snippet, technically, it should work, but you leave
yourself in the dark as to what the server sees. i've used it on my server.

you appear to connect with the server, so i believe the posted data
is not what is expected.

with httpjob (i assume we're talking about okhttp, right?), testing for
success on the return isn't enough. you always need to test for failure.
the empty else block doesn't serve any purpose.
 
Last edited:
Upvote 0

Danie Steyn

Member
Licensed User
Longtime User
first you say "this then returns JSON data that I need to process".
now you talk about images ??? you're expecting images as json data??

if you are trying to mimic a webpage login that returns json data,
forget webview. you want httpjob.

if you are, in fact, logging into a webpage which is expecting posted
login credentials and which then displays images upon successful
login, then you want webview.

technically, it is certainly possible to download an html stream by posting
login credentials and then passing that stream to a webview, but if the
end result is to display images in a webview, what is the point of
accessing the resource with httpjob? just load the url in a webview with
posted login credentials and look at the images.

is bytesOfPOstRequest in the correct format expected by the server?
if the server is yours, you need to echo what was received and what was
expected. if the server isn't yours, you need an echoing test server. there
are some out there. you need to know why you're not logged in.

in your httppost snippet, you don't actually send the request. is this
actual code from your project? or are you paraphrasing?
also, assuming sending the request were actually there, where would
you put it? (before or after setheader?) also, your setheader request
is wrong. content-type has its own version: job1.GetRequest.SetContentType...

in your javaobject snippet, technically, it should work, but you leave
yourself in the dark as to what the server sees. i've used it on my server.

you appear to connect with the server, so i believe the posted data
is not what is expected.

with httpjob (i assume we're talking about okhttp, right?), testing for
success on the return isn't enough. you always need to test for failure.
the empty else block doesn't serve any purpose.
Thanks for the reply. I will try explain in a bit more details.
I am trying to display a webpage, that requires me to login using postdata in JSON format. The login source block I posted on my original message is my VB.net code and that works perfectly, I've compared my bytesofpostrequest with the byte array in my vb.net app and they are the same. In the vb.net app the website responds and I handle the response and print receipts, will worry about that later, at the moment I just want to get the page displayed. The attached picture is what the user see's in the webbrowser control. Please excuse my code, empty else blocks etc is just because I am still busy trying to figure out a way to get this to work on B4A. I will try httppost with setcontenttype to see if that might help.
As you can clearly see I don't have much experience in this and I'm just trying different ways to see if I can get it working. Do you think an httpjob will be able to display in a webview?
You said "just load the url in a webview with posted login credentials and look at the images.", that is the reason for my post, I have no idea how to do this.
 

Attachments

  • Airtime.PNG
    Airtime.PNG
    152.8 KB · Views: 184
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Do you think an httpjob will be able to display in a webview?
You said "just load the url in a webview with posted login credentials and look at the images.", that is the reason for my post, I have no idea how to do this.
httpjob (by itself) does not have anything to do with a webview. if you take the result of an httpjob (presumably, a string of bytes), you can do whatever you want with it. after the httpjob has completed.
the string of bytes could be anything (a mime type will clarify) if it happens to be something recognizable as html, you can take that string and load it into a webview for display. it is exactly the same as if you had an html file saved and you opened it and read it in as a string and loaded it into a webview. exactly the same. in fact, this happens to be exactly what a webview or any browser does: it gets a string of bytes from a server and renders it as html. httpjob just handles the downloading part. you're on your own after that.
httpjob download resource from server -----> string -------> webview.loadhtml( string )

the "code" you showed representing your try at httpjob was wrong. 1) there was no actual request made and 2)the content-type header was wrong. and 3) i'm guessing you may not know that the headers are set after the request is made. seems strange, i know. so try again and see what you get.

your javaobject snippet: jo.runmethod("postUrl", array .... works fine (i have used such a construct) assuming the posted data is not only correctly formed but is in the form expected by the server. this is where i believe the problem is. which is - of course - the very first comment from erel in his reply. you say - or at least imply - that it's correctly formatted...

so, get the httpjob code straightened out. if the posted data is good, the routine should work.
the javaobject call should work if the posted data is good.
either way, everything seems to point back to what's being posted.

if the server is yours, and you want to set up some temporary guest account, i'm happy to do a test. you can pm if you don't want dozens of people hammering your server.

and regarding your json string, the attached is how you create one in b4x
 

Attachments

  • Capture.PNG
    Capture.PNG
    29 KB · Views: 199
Last edited:
Upvote 0

Danie Steyn

Member
Licensed User
Longtime User
httpjob (by itself) does not have anything to do with a webview. if you take the result of an httpjob (presumably, a string of bytes), you can do whatever you want with it. after the httpjob has completed.
the string of bytes could be anything (a mime type will clarify) if it happens to be something recognizable as html, you can take that string and load it into a webview for display. it is exactly the same as if you had an html file saved and you opened it and read it in as a string and loaded it into a webview. exactly the same. in fact, this happens to be exactly what a webview or any browser does: it gets a string of bytes from a server and renders it as html. httpjob just handles the downloading part. you're on your own after that.
httpjob download resource from server -----> string -------> webview.loadhtml( string )

the "code" you showed representing your try at httpjob was wrong. 1) there was no actual request made and 2)the content-type header was wrong. and 3) i'm guessing you may not know that the headers are set after the request is made. seems strange, i know. so try again and see what you get.

your javaobject snippet: jo.runmethod("postUrl", array .... works fine (i have used such a construct) assuming the posted data is not only correctly formed but is in the form expected by the server. this is where i believe the problem is. which is - of course - the very first comment from erel in his reply. you say - or at least imply - that it's correctly formatted...

so, get the httpjob code straightened out. if the posted data is good, the routine should work.
the javaobject call should work if the posted data is good.
either way, everything seems to point back to what's being posted.

if the server is yours, and you want to set up some temporary guest account, i'm happy to do a test. you can pm if you don't want dozens of people hammering your server.

and regarding your json string, the attached is how you create one in b4x
Thanks. After Erel said my json format is wrong I did to it as in your picture here, got the same results.
I will send you my test credentials on a PM if you don't mind checking it would be greatly appreciated, I will just tinker a bit by myself to see if I can figure it our before bothering you.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
no problem. don't forget to show me your code (no paraphrasing) and the log messages you receive. i know it's hard to believe, but often what people say they have done is not what they have actually done.
i still don't know if the server is yours or not. if it's yours, i need to know what the server is expecting (show code, not description). if it's not yours, there must be documentation somewhere. unless your b4a code is wrong, and barring some as yet unseen factor, either of your proposed solutions should work. i use them both all the time...
 
Last edited:
Upvote 0
Top