jwz104
jwz104

Reputation: 367

delphi idhttpserver search parameter

I want to search/compair parameters(GET) in idhttpserver in Delphi. I do have a way to do it but I want to know if there is an easier way. This is the method i use now:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
para:string;
paraa:tstringlist;
begin
  for para in arequestinfo.params do
  begin
    paraa := tstringlist.Create;
    paraa.StrictDelimiter := True;
    paraa.Delimiter := '=';
    paraa.DelimitedText := para;
    if paraa[0] = '...' then
    begin
      if paraa[1] = '...' then
      begin
        ...
      end;
    end;
    paraa.Free;
  end;
end;

I am using delphi xe7

Upvotes: 0

Views: 1906

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598299

Params is a TStrings, which has indexed Names[] and ValueFromIndex[] properties:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  name, value: string;
  I: index;
begin
  for i := 0 to ARequestInfo.Params.Count-1 do
  begin
    Name := ARequestInfo.Params.Names[i];
    Value := ARequestInfo.Params.ValueFromIndex[i];
    if Name = '...' then
    begin
      if Value = '...' then
      begin
        ...
      end;
    end;
  end;
end;

Upvotes: 2

TLama
TLama

Reputation: 76753

In my view it's wasting of resources to use a string list to parse only param=value pairs. Even worse, you were creating and destroying a string list instance for each iterated parameter and missed to use the try..finally by which you were taking the risk of memory leaks. I would do something like this:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  Param: string;
  Parts: TArray<string>;
begin
  for Param in ARequestInfo.Params do
  begin
    Parts := Param.Split(['=']);
    // Parts[0] should now contain the parameter name
    // Parts[1] should now contain the parameter value
  end;
end;

Upvotes: 0

Related Questions