Form using HTTP POST and GET in Delphi

731 Views Asked by At

I use an HTTP component in Borland Delphi 3.0 (old version Delphi). No Indy TIdHTTP component available yet. Just using HTTP components.

I use POST and GET to process a form of registration for my program application (Radio Calculator v3.50). My program

The plan is here.

POST form:

Include Username, Email, Phone number, and JPG file (transfer to my account) --> there are 4 parameters.

First, I use POST for "processing" the different KEYs for every different Username. This is processed in my Web Server using PHP and Microsoft Access MDB as the database.

I use another HTTP process from Delphi (GET), a few minutes later (let's says 15 minutes), to get a Username and KEY of the registration person in their home, or wherever they are around the world.

They will get DATA automatically from my Web Server using PHP.

My questions:

  1. How can I use POST and GET commands in HTTP Delphi Components? With all 4 parameters I use simple coding here from a similar post:

    function PostExample: string; 
    var 
      lHTTP: TIdHTTP; 
      lParamList: TStringList; 
    begin 
      lParamList := TStringList.Create;
      lParamList.Add('id=1'); 
      lHTTP := TIdHTTP.Create; 
    
      try 
        Result := lHTTP.Post('http://blahblahblah...', lParamList); 
      finally 
        lHTTP.Free; 
        lParamList.Free; 
      end; 
    end;
    

    How can I process the POST and GET result? What variables will be filled with this syntax? And how can I use this for further processing?

  2. Will this procedure work fine, or is there some leakage in securities in it?

  3. I use POST HTTP, sending these parameters, process it using PHP in my Web Server, and I get the raw result using GET a few minutes later. Is there anything I missed in the procedure? Should GET be instantly requested after POST, or how should I do it? Should I change HTTP to TWebBrowser only? Just make a Web Browser in Delphi? So I don't have to think hard about the HTTP process, only use my PHP programming competency instead?

How coding uses TIdHTTP.Post() to PHP:

procedure TForm1.Button1Click(Sender: TObject);
Var
  params : TStringList;
  res : TStringStream;
begin
  params := TStringList.Create;
  params.Add('a='+'hello');
  res := TStringStream.Create('');
  idhttp1.Post('http://localhost/test/hallo.php', params, res);
  ShowMessage(res.DataString);
  res.Free;
  params.Free;
end;
<?php
echo $_POST['a'];
%>

i ve UPLOAD IT IN GITHUB NGAB. Here 100% Works

1

There are 1 best solutions below

5
On

Delphi 3 predates Indy by quite a few years. Before Indy, Delphi shipped with a VCL component suite known as FastNet (later NetMasters), but I don't know if they go all the way back to Delphi 3 or not. Before FastNet, Delphi shipped with a suite of ActiveX controls from NetManage.

If you are using NetManage's HTTP ActiveX control, then I can't help you. I have no information on that control at all.

If you are using FastNet/NetMasters TNMHTTP component, then the Indy code you have shown would probably translate into something like the following for TNMHTTP (the FastNet/NetMaster components have been dead for a very long time, and there is no surviving documentation for them, so I'm basing this solely on looking at the NMHTTP.hpp C++ header file shipped in C++Builder v5, since no FastNet/NetMasters source code was provided with Delphi/BCB, or that I have access to, anyway):

procedure TForm1.Button1Click(Sender: TObject);
begin
  NMHTTP1.Post('http://localhost/test/hallo.php', 'a=hello');
end;

// NHTTP1.OnSuccess event handler
procedure TForm1.NMHTTP1Success(Cmd: CmdType);
begin
  ShowMessage('Success: ' + NMHTTP1.Body);
end;

// NHTTP1.OnFailure event handler
procedure TForm1.NMHTTP1Failure(Cmd: CmdType);
begin
  ShowMessage('Failed');
end;

Alternatively:

type
  TMyNMHTTP = class(TNMHTTP)
  public
    constructor Create(AOwner: TComponent); override;
    procedure HTTPSuccess(Cmd: CmdType);
    procedure HTTPFailure(Cmd: CmdType);
  end;

constructor TMyNMHTTP.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  OnSuccess := HTTPSuccess;
  OnFailure := HTTPFailure;
end;

procedure TMyNMHTTP.HTTPSuccess(Cmd: CmdType);
begin
  Tag := 1;
end;

procedure TMyNMHTTP.HTTPFailure(Cmd: CmdType);
begin
  Tag := 2;
end;

function PostExample: string; 
var 
  lHTTP: TMyNMHTTP;
begin 
  Result := '';
  lHTTP := TMyNMHTTP.Create(nil);
  try 
    lEvents.Tag := 0;
    lHTTP.Post('http://blahblahblah...', 'id=1');
    repeat
      Application.ProcessMessages;
    until lHTTP.Tag <> 0;
    if lHTTP.Tag = 1 then
      Result := lHTTP.Body;
  finally 
    lHTTP.Free; 
  end; 
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Response: string;
begin
  Response := PostExample();
  if Response <> '' then
    ShowMessage('Success: ' + Response)
  else
    ShowMessage('Failed');
end;