简体   繁体   English

delphi idhttpserver搜索参数

[英]delphi idhttpserver search parameter

I want to search/compair parameters(GET) in idhttpserver in Delphi. 我想在Delphi的idhttpserver中搜索/修复参数(GET)。 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 我正在使用delphi xe7

Params is a TStrings , which has indexed Names[] and ValueFromIndex[] properties: Params是一个TStrings ,具有索引的Names[]ValueFromIndex[]属性:

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. 在我看来,使用字符串列表仅解析param=value对会浪费资源。 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. 更糟糕的是,您正在为每个迭代的参数创建和销毁字符串列表实例,并且错过了使用try..finally ,因为这样做最终冒着内存泄漏的风险。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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