简体   繁体   English

使用 delphi 5 解析 Url 查询字符串

[英]Parse Url querystring with delphi 5

I have this string:我有这个字符串:

Called=%2B12608883475&ToState=IN&CallerCountry=US&Direction=inbound&CallerState=IN&ToZip=&CallSid=CAb7faaa30058c2347a595630f2475113a&To=%2B12608883475&CallerZip= Called=%2B12608883475&ToState=IN&CallerCountry=US&Direction=inbound&CallerState=IN&ToZip=&CallSid=CAb7faaa30058c2347a595630f2475113a&To=%2B12608883475&CallerZip=

I would like to parse it by & to get all the parameters out and then sort the parameters in an alphabetical case-sensitive order.我想通过&解析它以获取所有参数,然后按字母顺序对参数进行排序,区分大小写。

All of this has to be done in Delphi 5:所有这些都必须在 Delphi 5 中完成:

Function TwilioSignatureValidate(twilioSignature: string; QueryString: string ; Url: string): boolean;
begin
  parameters := QueryString;
  List := TStringList.Create;
end;

Here is the C# version that I want to copy:这是我要复制的 C# 版本:

string input = "Called=%2B12608883475&ToState=IN&CallerCountry=US&Direction=inbound&CallerState=IN&ToZip=&CallSid=CAb7faaa30058c2347a595630f2475113a&To=%2B12608883475&CallerZip=";

Dictionary<string,string> keyValuePairs = input.Split('&')
    .Select(value => value.Split('='))
    .ToDictionary(pair => pair[0], pair => pair[1]);

string studentId = keyValuePairs["StudentId"];

Using List.Sort;使用列表排序;

I got my Delphi List To sort alphebetically but its not in using Unix-style case-sensitive sorting order: //Delphi Sort https://ffbc1360.ngrok.io AccountSidACc6d06b4cb61ccbfa61bf461957a5a626 ApiVersion2010-04-01 Called+12608883475 CalledCity CalledCountryUS CalledStateIN CalledZip Caller+12602060582 CallerCity CallerCountryUS CallerStateIN CallerZip CallSidCAb7faaa30058c2347a595630f2475113a CallStatusringing Directioninbound From+12602060582 FromCity FromCountryUS FromStateIN FromZip To+12608883475 ToCity ToCountryUS ToStateIN ToZip I got my Delphi List To sort alphebetically but its not in using Unix-style case-sensitive sorting order: //Delphi Sort https://ffbc1360.ngrok.io AccountSidACc6d06b4cb61ccbfa61bf461957a5a626 ApiVersion2010-04-01 Called+12608883475 CalledCity CalledCountryUS CalledStateIN CalledZip Caller+ 12602060582 CallerCity CallerCountryUS CallerStateIN CallerZip CallSidCAb7faaa30058c2347a595630f2475113a CallStatusringing Directioninbound From+12602060582 FromCity FromCountryUS FromStateIN FromZip To+12608883475 ToCity ToCountryUS ToStateIN ToZip

//Correct c# Sort https://ffbc1360.ngrok.io AccountSidACc6d06b4cb61ccbfa61bf461957a5a626 ApiVersion2010-04-01 CallSidCAb7faaa30058c2347a595630f2475113a CallStatusringing Called+12608883475 CalledCity CalledCountryUS CalledStateIN CalledZip Caller+12602060582 CallerCity CallerCountry USCaller StateIN CallerZip Directioninbound From+12602060582 FromCity FromCountryUS FromStateIN FromZip To+12608883475 ToCity ToCountryUS ToStateIN ToZip //Correct c# Sort https://ffbc1360.ngrok.io AccountSidACc6d06b4cb61ccbfa61bf461957a5a626 ApiVersion2010-04-01 CallSidCAb7faaa30058c2347a595630f2475113a CallStatusringing Called+12608883475 CalledCity CalledCountryUS CalledStateIN CalledZip Caller+12602060582 CallerCity CallerCountry USCaller StateIN CallerZip Directioninbound From+12602060582 FromCity FromCountryUS FromStateIN FromZip To+12608883475 ToCity ToCountryUS ToStateIN ToZip

TStringList in Delphi 6 and later has Delimiter , DelimitedText , and CaseSensitive properties. Delphi 6 及更高版本中的TStringList具有DelimiterDelimitedTextCaseSensitive属性。 Set the Delimiter to '&' and then assign the query string to DelimitedText , then you can use the Values[] property to extract values by name (since the resulting delimited strings are already in name=value format).Delimiter设置为'&' ,然后将查询字符串分配给DelimitedText ,然后您可以使用Values[]属性按名称提取值(因为生成的分隔字符串已经采用name=value格式)。 No sorting is needed:不需要排序:

Function TwilioSignatureValidate(twilioSignature: string; QueryString: string ; Url: string): boolean;
var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    List.Delimiter := '&';
    List.DelimitedText := QueryString;
    List.CaseSensitive := True;

    // use List.Values['param name'] as needed, eg:
    // List.Values['Called']
    // List.Values['ToState']
    // List.Values['CallerCountry']
    // List.Values['Direction']
    // List.Values['CallerState']
    // List.Values['ToZip']
    // List.Values['CallSid']
    // List.Values['To']
    // List.Values['CallerZip']
  finally
    List.Free;
  end;
end;

However, in Delphi 5, none of those properties existed yet, so you will have to do everything manually:但是,在 Delphi 5 中,这些属性都不存在,因此您必须手动完成所有操作:

Function TwilioSignatureValidate(twilioSignature: string; QueryString: string ; Url: string): boolean;
var
  List: TStringList;

  // the following are tweaked from TStrings in later Delphi versions...

  procedure MySetDelimitedText(const Value: string);
  var
    P, P1: PChar;
    S: string;
  begin
    List.BeginUpdate;
    try
      List.Clear;
      P := PChar(Value);
      while P^ <> #0 do
      begin
        if P^ = '"' then
          S := AnsiExtractQuotedStr(P, '"')
        else
        begin
          P1 := P;
          while (P^ <> #0) and (P^ <> '&') do
            P := CharNext(P);
          SetString(S, P1, P - P1);
        end;
        List.Add(S);
        if P^ = '&' then
        begin
          P := CharNext(P);
          if P^ = #0 then
            List.Add('');
        end;
      end;
    finally
      List.EndUpdate;
    end;
  end;

  function MyIndexOfName(const Name: string): Integer;
  var
    P: Integer;
    S: string;
  begin
    for Result := 0 to List.Count - 1 do
    begin
      S := List.Strings[Result];
      P := AnsiPos('=', S);
      if (P <> 0) and (AnsiCompareStr(Copy(S, 1, P - 1), Name) = 0) then Exit;
    end;
    Result := -1;
  end;

  function MyGetValue(const Name: string): string;
  var
    I: Integer;
  begin
    I := MyIndexOfName(Name);
    if I >= 0 then
      Result := Copy(List.Strings[I], Length(Name) + 2, MaxInt) else
      Result := '';
  end;

begin
  List := TStringList.Create;
  try
    MySetDelimitedText(QueryString);

    // use MyGetValue('param name') as needed, eg:
    // MyGetValue('Called')
    // MyGetValue('ToState')
    // MyGetValue('CallerCountry')
    // MyGetValue('Direction')
    // MyGetValue('CallerState')
    // MyGetValue('ToZip')
    // MyGetValue('CallSid')
    // MyGetValue('To')
    // MyGetValue('CallerZip')
  finally
    List.Free;
  end;
end;

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

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