Recent

Author Topic: fpHttpClient timeout formPost(URL, Params , Response)  (Read 6163 times)

jur

  • New member
  • *
  • Posts: 7
fpHttpClient timeout formPost(URL, Params , Response)
« on: January 10, 2017, 01:38:30 am »
Hi,
I want to use the fpHttpclient.FormPost(URL, Params , Response) to connect to a php script on my webserver.

I am using a thread, but sometimes the formPost in hte thread hangs, and I dont want to kill a hanging thread (memory leak).

I see versions of the fpHttp client on internet with timeout, but I cant get these versions up and working.

Regards, Jur

balazsszekely

  • Guest
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #1 on: January 10, 2017, 05:35:55 pm »
Hi jur,

Take a look at the following units:
   http://svn.freepascal.org/svn/lazarus/trunk/components/onlinepackagemanager/fpcmod/opkman_httpclient.pas
and
   http://svn.freepascal.org/svn/lazarus/trunk/components/onlinepackagemanager/opkman_uploader.pas

It's basically what you need. A threaded post to a php file with the possibility to cancel the operation.

jur

  • New member
  • *
  • Posts: 7
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #2 on: January 10, 2017, 10:22:33 pm »
Hi GetMEM,

Thanks for the link. I am not sure how to use the timeout though.
The code I am using wont time-out after the timeout delay (using a sleep(15)  sec in PHP script before echoing the result. Correct values are sent, after 15 s, but no time-out occurs.

     with TFPHTTPCLIENT.Create(Nil)do
     begin
          IOtimeout := 1 ;
          Response := TStringStream.Create('');
          FormPost(URL, Params , Response);
          memo1.Lines.Insert(0,response.datastring);
    end ;

Regards, Jur

balazsszekely

  • Guest
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #3 on: January 10, 2017, 10:34:22 pm »
Quote
Thanks for the link. I am not sure how to use the timeout though.
The code I am using wont time-out after the timeout delay (using a sleep(15)  sec in PHP script before echoing the result. Correct values are sent, after 15 s, but no time-out occurs.
First of all you have to copy opkman_httpclient.pas to your project folder, rename it to whatever you like, basically is the same unit you can find in FPC, but it's modified to exit immediately when FPHTTPClient.NeedToBreak is set to true. Secondly take a look at opkman_uploader which contains a TUploader class, basically a thread designed to call a php function(see function PostFile). Now when you want to cancel the PostFile, just call TUploader.StopUpload, it will exit even if the remote server is not responding. You may have to modify a few parameters since you don't call the same php as I do, but the idea is the same.

jur

  • New member
  • *
  • Posts: 7
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #4 on: January 10, 2017, 11:36:00 pm »
Hi,

Thanks for the help. It works in my test app using the  FPHTTPClient.NeedToBreak.
The timeout is not working I assume, so I will have to build a thread in my program, and check/break hanging posts

regards,  Jur

balazsszekely

  • Guest
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #5 on: January 11, 2017, 06:31:56 am »
@jur
Quote
Thanks for the help. It works in my test app using the  FPHTTPClient.NeedToBreak.
You're welcome!

Quote
The timeout is not working I assume, so I will have to build a thread in my program, and check/break hanging posts
Yes. You need to implement a counting mechanism, after X seconds just set FPHTTPClient.NeedToBreak to true and exit your thread.

jur

  • New member
  • *
  • Posts: 7
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #6 on: January 15, 2017, 03:29:40 pm »
Hi GetMEM,

I have ported my working code from windows to a raspberry Pi (recent version of rasbian jessie, lazarus/fpc), using the opkman_httpclient.pas

To my surprise on the raspberry, the FormPost(URL, Params , Response) is not blocking at all. It works ok (uploading data and getting response) but when I introduce a sleep for 1 or more seconds on my webpage, the formpost does not wait, en no respons is received.

I'm not sure how this works. Any suggestions?

regards, JUr


 

balazsszekely

  • Guest
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #7 on: January 15, 2017, 03:58:58 pm »
Hi jur,

You should debug the code to find out the reason, unfortunately I don't have raspberry PI. Under windows it works if you introduce a sleep on the wepage?

Ps: Why you want to wait on server side?
« Last Edit: January 15, 2017, 04:03:41 pm by GetMem »

jur

  • New member
  • *
  • Posts: 7
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #8 on: January 15, 2017, 04:31:10 pm »
Hi GetMEM,

If I introduce a sleep on the webpage, then in windows the formpost just waits until the results are returned (after the sleep). On the Raspberry, if the sleep is longer then the timeout (approx 1 s I think) the formPost ends without returning results.

I did introduce a sleep on the webpage to simulate the effect of a slow connection, or a slow query on the database producing the results.

Where do I start looking for this (fixed?) timeout?

Regards, Jur
 

balazsszekely

  • Guest
Re: fpHttpClient timeout formPost(URL, Params , Response)
« Reply #9 on: January 15, 2017, 05:05:23 pm »
Quote
If I introduce a sleep on the webpage, then in windows the formpost just waits until the results are returned (after the sleep). On the Raspberry, if the sleep is longer then the timeout (approx 1 s I think) the formPost ends without returning results.
Then it's a bug under raspberry and you should fill in a bug report. Please attach a simple demo that demonstrates the issue. I don't know when will be fixed though(if ever).

 

TinyPortal © 2005-2018