I have a similar question here, but it was oriented to PHP side. It looks like PHP is able to receive a package of data and immediately (in the same session) send and answer back. My question is can Delphi do that? From what I know the answer is a big NO. I need to do this in two steps (two procedures). The thing is that the session will be probably closed when the first procedure ends. Can I keep the session open between the two procedure calls.
-
I don't see why Delphi would struggle with this. It just sounds like a simple HTTP get. Your question would benefit from you spending the time to explain exactly what is going on. – David Heffernan Feb 19 '11 at 12:30
-
1Why? It is true PHP style - do *everything* in the single sluggish script. You probably want POST, but i dont want to guess. Provide more detail on what you doing. – Free Consulting Feb 19 '11 at 12:40
-
What session? What procedure calls? Give more details! – Cosmin Prund Feb 19 '11 at 19:16
1 Answers
I'm going to give you sample code for both PHP
and Delphi
. We're going to use the GET
request method because it's much easier and it's enough if you don't need to send too much data. I'm going to start with the PHP
script, because it can be tested alone, without the Delphi
app (we'll test it using an Browser).
Here's the PHP script:
<?php
$a=$_GET['a'];
$b=$_GET['b'];
echo intval($a)*intval($b);
?>
This scripts expects two values sent encoded in the URL (a and b), multiplies the values and returns the answer. Assuming you're playing on the computer that's running LAMP, and you called this script script.php
, if you navigate to this on your browser:
http://localhost/script.php?a=2&b=3
You'll see this in your browser:
6
To break down the URL we're writing in the browser: we're requesting the page /script.php
from the server localhost
and we're passing this query string to the script: a=2&b=3
. The stuff that goes after the ?
in the URL is the query string; The &
symbol separates the two separate parameter=value pairs: a=2
and b=3
. On the PHP side you use $a=$_GET['a']
to get the value of the parameter a
. It's that simple!
Delphi code
Now that we know the PHP script is up and running, we can move on to the Delphi part. Here's a routine that multiplies two values using the most inefficient method imaginable:
function MultiplyTwoNumbers(a,b:Integer):Integer;
var url: string;
H: TIdHttp;
SS: TStringStream;
begin
// Prepare the URL
url := 'http://fisiere.sediu.ro/script.php?a=' + IntToStr(a) + '&b=' + IntToStr(b);
H := TIdHttp.Create(nil);
try
SS := TStringStream.Create;
try
H.Get(url, SS);
Result := StrToInt(SS.DataString);
finally SS.Free;
end;
finally H.Free;
end;
end;
I intentionally left my proper working URL in there, so you can actually test against my known-to-work script.
Here's how you'd call this routine:
procedure TForm8.Button1Click(Sender: TObject);
begin
ShowMessage(IntToStr(MultiplyTwoNumbers(3,4)));
end;
Summary
- HTTP is stateless, people go to lots of trouble to maintain some kind of state. Your
Delphi app makes two calls, first to SendID then a second to GetDataBack
idea doesn't work, because the thing running on the server when you call (actually GET)GetDataBack
has no idea someone sometime called (GET
again)SendID
. - Happily when
GET
-ing data from an HTTP server we can use the query string to easily send some information. If we need to send a lot of information we'd use thePOST
requests.

- 601,492
- 42
- 1,072
- 1,490

- 25,498
- 2
- 60
- 104
-
Thanks a lot Cosmin. This seems to be what I need. I will test it right now. +1 – Gabriel Feb 19 '11 at 20:29
-
2There is no need for a stringstream. This works too: Result := H.Get(url); – Jan Derk Feb 20 '11 at 16:08
-
Also the server can use sessions with TidHttp. So you are not completely stateless between multiple calls. That is as long as you don't delete the TidHttp between the two calls of course. – Jan Derk Feb 20 '11 at 16:15
-
"There is no need for a stringstream. This works too" - - - Get is a procedure (at least in Indy 10). – Gabriel Mar 15 '11 at 09:57
-
Added code to set UserAgent and avoid '403 Forbidden' error. Issue is mentioned here on StackOverflow: http://stackoverflow.com/questions/10870730/why-do-i-get-403-forbidden-when-i-connect-to-whatismyip-com – TheSteven Apr 07 '14 at 22:38
-
Cosmin - mai esti prin zona? As vrea tare mult sa vorbesc cu tine 10 minute pe ceva asemanator, daca binevoiesti. Imi poti lasa un contact? (mail / facebook?) sterg comentariul dupa. Mersi frumos! – t1f Dec 05 '16 at 16:26