简体   繁体   中英

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

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM