HTTP library v1.5 - Asynchronous calls

konisek

Member
Licensed User
Longtime User
Sorry, I still don't understand it how to read data into array asynchronously. I looked at the help file but still am not sure. Is this the correct way?
B4X:
Sub Button1_Click
....
stream.WriteBytes(stream.StringToBytes(name))
response.Value = request.AsyncResponse
....
"buffer operation"
....
End Sub

Sub request_Response
Dim buffer(1024) As byte
count = bin.ReadBytes(buffer(),1024)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The stream is first saved to a file. Then you can open it with BinaryFile and read it to an array:
B4X:
Sub Globals
    Dim buffer(0) As byte
End Sub

Sub App_Start
    Form1.Show
    request.New1("http://www.b4x.com")
    response.New1
    request.GetAsyncResponse
End Sub

Sub request_Response
      If request.ResponseCode = 200 Then '200 is the code of successful connections.
            response.Value = request.AsyncResponse
            response.GetAsyncStream(AppPath & "\tempfile") 'Stream is saved to this file
      Else
            Msgbox("Error getting response: " & request.ErrorMessage & CRLF _
            & "Response code: " & request.ResponseCode)
      End If
End Sub

Sub response_Stream
    'file is ready
    FileOpen(c, AppPath & "\tempfile", cRandom)
    binaryFile.New1(c, False)
    Dim buffer(FileSize(AppPath & "\tempfile")) As byte
    Msgbox(binaryFile.ReadBytes(buffer(), ArrayLen(buffer())))
    FileClose(c)
    
End Sub
 

Attachments

  • 1.sbp
    310 bytes · Views: 16

konisek

Member
Licensed User
Longtime User
Thank you for the help.

Additionally, I tested your example HTTP-POST.sbp from the first page and discovered this:

1) When I run it on PC, I get the response immediately.
2) When I run it on PPC connected via wifi, I get the response in 39 seconds.
3) When I run it on PPC connected via gprs, I get the response also in 39 seconds.

I expected that the case (2) via wifi would have been much faster than case (3) but it is not. Is it just my case? Therefore I also tested how quickly the internet browser starts to show pages when I use wifi and then gprs. In both cases there is the same unappropriate delay from starting the internet icon upto actual show of the internet page on PDA screen. Maybe its only my case and my PDA needs a hard reset. Can anyone test and post the reply?
 
Last edited:

konisek

Member
Licensed User
Longtime User
I upgraded from WM6.1 to WM6.5 and the times shortened:
ad 2)...response in 6 seconds.
ad 3)...response in 6 seconds
:sign0060::sign0060::sign0060:
 

TWELVE

Active Member
Licensed User
Hi Erel,

for certain reasons, i changed all my http stuff to UTF-8, which works fine on desktop, but returns error from server on device.

After some testing and sniffing with wireshark i believe there's a bug in the http.dll on the device.Earlier, i calculated the ContentLenght by myself because this was required.With newer version of http.dll ( not sure where it was introduced), i saw that it is not needed anymore to calculate the ContentLength, this was done by http.dll.This works fine, but it looks like, that the device http.dll does not calculate correctly as soon as UTF-8 comes into the play.This is even true if no UTF-8 char is sent.

The only difference between desktop(working) and device(not working) was the Content-Length header, there is a difference of 6 bytes in length ( device 6 bytes less than on desktop).


Can you have a look into that matter..?


regards,

TWELVE
 

TWELVE

Active Member
Licensed User
Hi Erel,

thanks for the reply.After some more debugging it turned out that i hit rare bug in our server scripts, for some reasons i can explain now it only occured on the device then.

So no further action required from your side...;-)

regards,

TWELVE
 

directorfw

Member
Licensed User
Longtime User
Erel!

I'm using a simple php file that prints the POST variables.
B4X:
Sub Globals

End Sub

Sub App_Start
    Form1.Show
End Sub

Sub Button1_Click
    request.New1("http://www.b4x.com/print.php")
    request.Method = "POST"
    Request.ContentType = "application/x-www-form-urlencoded"
    stream.New1(Request.GetStream,True) 
    name = "First=Erel&Last=Uziel"
    stream.WriteBytes(stream.StringToBytes(name))
    response.New1
    request.GetAsyncResponse
End Sub

Sub request_Response
    If request.ResponseCode = 200 Then
        response.Value = request.AsyncResponse
        response.GetAsyncString
    Else
        Msgbox(request.ErrorMessage)
    End If
End Sub

Sub response_Stream
    TextBox1.Text = response.AsyncString
End Sub

could you show your php code? please?
:sign0085:
 

sitajony

Active Member
Licensed User
I think that it's:
B4X:
<?
$first=$_POST["First"];
$last=$_POST["Last"];
echo $first. " " .$last;
?>
 
Top