PureBasic Forum
https://www.purebasic.fr/english/

[Done] HTTPRequest() and asynchronous
https://www.purebasic.fr/english/viewtopic.php?f=4&t=72280
Page 1 of 1

Author:  infratec [ Wed Feb 13, 2019 7:27 pm ]
Post subject:  [Done] HTTPRequest() and asynchronous

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

Author:  Fred [ Mon Mar 18, 2019 8:39 pm ]
Post subject:  Re: HTTPRequest() and asynchronous

Fixed.

Author:  rotacak [ Thu Jul 04, 2019 7:36 pm ]
Post subject:  Re: [Done] HTTPRequest() and asynchronous

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

Author:  infratec [ Sat Jul 06, 2019 11:02 am ]
Post subject:  Re: [Done] HTTPRequest() and asynchronous

@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.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/