It is currently Sun Dec 08, 2019 6:11 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: [Done] HTTPRequest() and asynchronous
PostPosted: Wed Feb 13, 2019 7:27 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4447
Location: Germany
Hi,

I need asynchronous to fix the missing timeout.
But if asynchronous is used, the string is not correctly transmitted.

Code:
EnableExplicit


Procedure Server(Dummy.i)
 
  Protected Server.i, Client.i, RcvLen.i, Pos.i, Rcv$, *Buffer
 
 
  Server = CreateNetworkServer(#PB_Any, 3333)
  If Server
   
    *Buffer = AllocateMemory(10240)
    If *Buffer
     
      Repeat
        Select NetworkServerEvent()
          Case #PB_NetworkEvent_Data
            Debug "Data"
            CLient = EventClient()
            RcvLen = ReceiveNetworkData(Client, *Buffer, MemorySize(*Buffer))
            If RcvLen
              Debug PeekS(*Buffer, RcvLen, #PB_UTF8|#PB_ByteLength)
              ShowMemoryViewer(*Buffer, RcvLen)
              Delay(5000)
              Debug "Sending answer"
              SendNetworkString(Client, "HTTP/1.1 200 Ok" + #CRLF$ + "Content-Length: 0" + #CRLF$ + #CRLF$)
            EndIf
           
          Case #PB_NetworkEvent_None
            Delay(10)
        EndSelect
      ForEver
     
      FreeMemory(*Buffer)
     
    EndIf
   
  EndIf
 
EndProcedure


Define Test$, *Test, HTTPRequest.i, Progress.i, Timeout.i, Starttime.i
Define NewMap Header$()

InitNetwork()


CreateThread(@Server(), #Null)

Test$ = "Hello world!"
Header$("Content-Type") = "plaintext"

Debug "Tx 1"
HTTPRequest = HTTPRequest(#PB_HTTP_Post, "http://localhost:3333", Test$, 0, Header$())
If HTTPRequest
  FinishHTTP(HTTPRequest)
EndIf

Debug ""

Debug "Tx 2"
HTTPRequest = HTTPRequest(#PB_HTTP_Post, "http://localhost:3333", Test$, #PB_HTTP_Asynchronous, Header$())
If HTTPRequest
 
  Timeout = 3
  Starttime = Date()
 
  Repeat
    Progress = HTTPProgress(HTTPRequest)
    Select Progress
      Case #PB_HTTP_Success
        Break
      Case #PB_HTTP_Failed
        Break
      Case #PB_HTTP_Aborted
        Break
      Default
        Delay(10)
        If Date() - Starttime > Timeout
          Debug "Timeout"
          AbortHTTP(HTTPRequest)
          Break
        EndIf
       
    EndSelect
  ForEver
 
  FinishHTTP(HTTPRequest)
 
EndIf


If I use HTTPRequestMemory() it works, if I don't free the buffer until the request is finished.

PB 5.70 x86 on win 10 x64

@Fred:
Do you use CURLOPT_COPYPOSTFIELDS?
Else the buffer has to be preserverd.

Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: HTTPRequest() and asynchronous
PostPosted: Mon Mar 18, 2019 8:39 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13643
Location: France
Fixed.


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] HTTPRequest() and asynchronous
PostPosted: Thu Jul 04, 2019 7:36 pm 
Offline
User
User

Joined: Tue Feb 14, 2006 2:00 pm
Posts: 63
It is really fixed?

This will not send request:
Code:
  HttpRequest = HTTPRequest(#PB_HTTP_Post, "http://...", "aaa", #PB_HTTP_Asynchronous)
  If HttpRequest
    FinishHTTP(HTTPRequest)
  EndIf


But this yes:
Code:
  HttpRequest = HTTPRequest(#PB_HTTP_Post, "http://...", "aaa", #PB_HTTP_Asynchronous)
  If HttpRequest
    ;FinishHTTP(HTTPRequest)
  EndIf


5.71 beta 2 LTS win 10 32bit


Top
 Profile  
Reply with quote  
 Post subject: Re: [Done] HTTPRequest() and asynchronous
PostPosted: Sat Jul 06, 2019 11:02 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4447
Location: Germany
@rotacak

You did not understand what asynchronous does.

In short:
HTTPRequest() returns imediately and you can check the transfer stuff.
Maybe to implement a progressbar.

So if you call FinishRequest() directly after, the request is terminated.
Nothing is send, because it was no time to send something.

Definatley your code shows no bug. It shows normal behaviour.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye