簡體   English   中英

delphi idhttpserver搜索參數

[英]delphi idhttpserver search parameter

我想在Delphi的idhttpserver中搜索/修復參數(GET)。 我確實有這樣做的方法,但是我想知道是否有更簡單的方法。 這是我現在使用的方法:

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;

我正在使用delphi xe7

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;

在我看來,使用字符串列表僅解析param=value對會浪費資源。 更糟糕的是,您正在為每個迭代的參數創建和銷毀字符串列表實例,並且錯過了使用try..finally ,因為這樣做最終冒着內存泄漏的風險。 我會做這樣的事情:

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