B4J Question HTTP POST request when loading webpage

ThRuST

Well-Known Member
Licensed User
Longtime User
I want to open a webpage on client to view the changes on the webpage when posting data to the server.
How can the webpage be displayed together with a HTTP POST request when the values is delivered from B4J?

The following code just works in the background and then PHP handles the values on the server.
A workaround is to send a HTTP GET request in the URL which works, but I prefer to use the POST method to hide the data being sent.

B4X:
Sub SendPostRequest
  Dim val1 As String = "Hello"
  Dim val2 As String = "World"

    Private sendjob1 As HttpJob

    sendjob1.Initialize("", Me)
    'Send a POST request
    sendjob1.PostString("https://www.mydomain.com/mypage.php", _
    "val1=" & myVariable1 & _
    "&val2=" & myVariable2)


    Wait For (sendjob1) JobDone(sendjob1 As HttpJob)

    If sendjob1.Success Then  
        xui.MsgboxAsync("Data was successfully sent to server.", "")
   
        ' How to display the webpage with changes in this process?
   
    End If

    sendjob1.Release

End Sub
 
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
@aeric HTTP POST doesn't open the page. Compared to HTTP GET below (which works). Any clues how to make this work when using POST request?

B4X:
fx.ShowExternalDocument("https://www.mydomain.com/page.php?val1=" & myVariable1 & "&val2=" & myVariable2)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@aeric You mean the server must redirect to another webpage with HTTP POST for this to work? As long as the variables are not lost in the process.
Thanks for your response on this šŸ‘
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I still don't understand. Do you want to GET the response (HTML) back?
Try:
B4X:
sendjob1.Download2("https://www.mydomain.com/mypage.php", Array As String("val1", myVariable1, "val2", myVariable2))
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@aeric My example in post #3 should explain what I want to do. View the webpage once the two variables are delivered to the server.
The code example in post #1 only sends the strings. Unlike post #3, the page is opened on the client together with the page that process the passed values.
There's no need to return the values since they only should be sent to the server and view the page in the process.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I think the only solution is to store the incoming POST variables and redirect to another page. The server has to open the page once the values arrives.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
How about using a webview to show the page?

B4X:
Sub SendPostRequest
    Dim val1 As String = "Hello"
    Dim val2 As String = "World"
    Dim response As String
    
    Private sendjob1 As HttpJob

    sendjob1.Initialize("", Me)
    'Send a POST request
    sendjob1.PostString("http://localhost/demo2/mypage.php", _
    "val1=" & val1 & _
    "&val2=" & val2)
    
    Wait For (sendjob1) JobDone(sendjob1 As HttpJob)

    If sendjob1.Success Then
        xui.MsgboxAsync("Data was successfully sent to server.", "")
  
        ' How to display the webpage with changes in this process?
        response = sendjob1.GetString         
    Else
        response = sendjob1.ErrorMessage
    End If
    WebView1.LoadHtml(response)
    sendjob1.Release   
End Sub

mypage.php
PHP:
<?php
    $val1 = $_POST["val1"];
    $val2 = $_POST["val2"];

    $body = "<h1>".$val1."</h1>";
    $body .= "<p>".$val2."</p>";
    echo $body;
?>
 

Attachments

  • demo2.zip
    2.3 KB · Views: 156
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@aeric Thanks. A webview is not what I need to use in this case, so I'll give redirect to another webpage a try. Once a temporary page handles the incoming variables they should be kept and passed to the page that opens from serverside. That should work as a functional workaround. Or perhaps just encode the strings being sent in HTTP GET together with the URL. I'll evaluate the prefered methods.
 
Upvote 0
Top