如果我们试图在 TIdHTTP 中以中文单词为参数执行 GET,我们需要什么特殊处理

What special handling do we need if we are trying to execute GET in TIdHTTP with Chinese words as parameters

我正在尝试使用 TIdHTTO 在 Delphi 中获取以下 URL,但失败了:

https://api.sketchengine.eu/bonito/run.cgi/wsketch?lemma=上海&username=agofpos&api_key=ce39ab3f07544e759b068338ac1974e2&corpname=preloaded/zhtenten17_simplified_stf2&format=json

不过使用 POSTMAN 没问题。

这是我的代码:

url := 'https://api.sketchengine.eu/bonito/run.cgi/wsketch?lemma=上海&username=agofpos&api_key=ce39ab3f07544e759b068338ac1974e2&corpname=preloaded/zhtenten17_simplified_stf2&format=json';
memoResult.Text := idHttp.Get(url);

GET过程中处理非英文字符需要特殊处理吗?如果是,怎么做?

我正在使用 Delphi 10.2,如果有帮助的话。

URLs 不允许非 ASCII 字符(IRI 允许,但尚未广泛使用)。在 URL 中,您必须对任何非 ASCII 字符(和其他保留的)字符进行 url 编码,例如 Indy 的 TIdURI class,例如:

uses
  ..., IdURI;

url := 'https://api.sketchengine.eu/bonito/run.cgi/wsketch?lemma=' + TIdURI.ParamsEncode('上海') + '&username=agofpos&api_key=ce39ab3f07544e759b068338ac1974e2&corpname=preloaded/zhtenten17_simplified_stf2&format=json';
memoResult.Text := idHttp.Get(url);

这会将中文字符编码为UTF-8,然后将这些字节编码为%HH格式,例如:

https://api.sketchengine.eu/bonito/run.cgi/wsketch?lemma=%E4%B8%8A%E6%B5%B7&username=agofpos&api_key=ce39ab3f07544e759b068338ac1974e2&corpname=preloaded/zhtenten17_simplified_stf2&format=json

注意 上海 是如何变成 %E4%B8%8A%E6%B5%B7 的。

Web 浏览器、POSTMAN 等会在内部为您处理此问题。您可以通过查看实际传输的原始 HTTP 请求来确认这一点。