您需要先将 URL 解析为其组件,然后才能对参数进行解码。
Url := 'https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=delphi+url+parameters+';
Params := TStringList.Create;
try
Params.Delimiter := '&';
Params.StrictDelimiter := true;
Uri := TIdURI.Create(Url);
try
Params.DelimitedText := Uri.Params;
finally
Uri.Free;
end;
for i := 0 to Params.Count -1 do
begin
Params.Strings[i] := StringReplace(Params.Strings[i], '+', ' ', [rfReplaceAll]);
Params.Strings[i] := TIdURI.URLDecode(Params.Strings[i], enUtf8);
end;
// use Params as needed...
finally
Params.Free;
end;
要创建一个新的 URL,只需反转该过程:
Params := TStringList.Create;
try
// fill Params as needed...
for i := 0 to Params.Count -1 do
begin
Params.Strings[i] := TIdURI.ParamsEncode(Params.Names[i], enUtf8) + '=' + TIdURI.ParamsEncode(Params.ValueFromIndex[i], enUtf8);
Params.Strings[i] := StringReplace(Params.Strings[i], ' ', '+', [rfReplaceAll]);
end;
Params.Delimiter := '&';
Params.StrictDelimiter := true;
Uri := TIdURI.Create('');
try
// fill other Uri properties as needed...
Uri.Params := Params.DelimitedText;
URL := Uri.URI;
finally
Uri.Free;
end;
finally
Params.Free;
end;