Raffaele Rossi
Raffaele Rossi

Reputation: 3127

Delphi indy call php script

I have a php script in my server that is very simple because he has a single task: store in a sql database two data.

http://mydomain.it/test.php?id=285&numero=504;

If you open this url in the browser, you are going to add a row in an sql table that looks like this

enter image description here

I want to be able to store the data in the database using firemonkey too, and I have placed in my app two TEdits and a TButton. Clicking on the button this code executes:

var url: string;
 H: TIdHttp;
 SS: TStringStream;
begin
 url := 'http://mydomain.it/test.php?id='+IntToStr(a)+'&numero='+IntToStr(b);
 H := TIdHttp.Create(nil);
 try
  SS := TStringStream.Create;
  try

   try
    H.Post(url, SS);
   except
    on E:Exception do
    ShowMessage(e.Message);
   end;

  finally
   SS.Free;
  end;
 finally 
  H.Free;
 end;

The code above works very well because it takes only 1 second to run and I am trapping the exceptions (just for a debug purpose). I have two main questions:

  1. This is the first time that I use indy and from what I have understood this is a good way to do what I need. I am not sure if I have placed the try-except in the correct place; is it?

  2. What about if internet connection is slow? Should I use something like the code below?

code

var process: array of ITask;
begin

 SetLength(process, 1);

 process[0] := TTask.Create(
  procedure
   var url: string;
    H: TIdHttp;
    SS: TStringStream;
   begin
    url := 'http://mydomain.it/test.php?id='+IntToStr(a)+'&numero='+IntToStr(b);
    H := TIdHttp.Create(nil);
    try
     SS := TStringStream.Create;
     try
      try
        H.Post(url, SS);
       except
        on E:Exception do
        ShowMessage(e.Message);
       end;
     finally
      SS.Free;
     end;
    finally H.Free;
    end;
  end
 );
 process[0].Start;

 TTask.WaitForAll(process);
 Label1.Text := 'done!!';

end;

The code in section 2 has some issue and it doesn't work. Any idea?

Upvotes: 3

Views: 2552

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596632

First, when you enter a URL in a browser, it sends a GET request, not a POST request. You should be using TIdHTTP.Get() instead of TIdHTTP.Post():

var
  url: string;
  H: TIdHTTP:
begin
  url := 'http://mydomain.it/test.php?id='+IntToStr(a)+'&numero='+IntToStr(b);
  try
    H := TIdHTTP.Create(nil);
    try
      H.Get(url);
      // or:
      // H.Get(url, TStream(nil));
    finally 
      H.Free;
    end;
  except
    on E: Exception do
      ShowMessage(e.Message);
  end;
end;

Now, to answer your questions:

Generally speaking, the try/except you showed is fine. I prefer to put the try/finally inside the try/except as shown above, rather than the other way around. But depending on where you actually use the code, a try/except may be redundant and could be omitted.

As for issues with Internet connectivity - generally, yes, you should perform your HTTP actions in a task/thread, not in the main UI thread:

begin
  TTask.Create(
    procedure
    var
      url, msg: string;
      H: TIdHTTP;
    begin
      try
        url := 'http://mydomain.it/test.php?id='+IntToStr(a)+'&numero='+IntToStr(b);
        H := TIdHTTP.Create(nil);
        try
          H.Get(url);
          // or:
          // H.Get(url, TStream(nil));
        finally
          H.Free;
        end;
        msg := 'done!!';
      except
        msg := 'error!!';
      end;
      TThread.Queue(nil,
        procedure
        begin
          Label1.Text := msg;
        end;
      );
    end
  ).Start;
end;

Upvotes: 6

Related Questions