Delphi TIdHTTP POST不编码加号 [英] Delphi TIdHTTP POST does not encode plus sign

查看:638
本文介绍了Delphi TIdHTTP POST不编码加号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个TIdHTTP组件,我正在向基于云的服务器发送一个http POST请求。除了1个字段外,所有内容都非常出色:带有加号的文本字符串,例如: 'hello world + dog',被保存为'hello world dog'。

I have a TIdHTTP component on a form, and I am sending an http POST request to a cloud-based server. Everything works brilliantly, except for 1 field: a text string with a plus sign, e.g. 'hello world+dog', is getting saved as 'hello world dog'.

研究这个问题,我意识到URL中的'+'被认为是空间,所以必须编码它。这是我难倒的地方;它看起来像POST请求的其余部分由TIdHTTP组件编码,除了'+'。

Researching this problem, I realise that a '+' in a URL is regarded as a space, so one has to encode it. This is where I'm stumped; it looks like the rest of the POST request is encoded by the TIdHTTP component, except for the '+'.

通过Fiddler查看请求,它将作为'你好%20world +狗。如果我手动编码'+'(hello world%2Bdog),结果是'hello%20world%252Bdog'。

Looking at the request through Fiddler, it's coming through as 'hello%20world+dog'. If I manually encode the '+' (hello world%2Bdog), the result is 'hello%20world%252Bdog'.

我真的不知道我是什么我在这里做,所以如果有人能指出我正确的方向,那将是非常感激的。

I really don't know what I'm doing here, so if someone could point me in the right direction it would be most appreciated.

其他信息:

我使用的是Delphi 2010.该组件没有任何特殊设置,我假设我可能需要设置一些东西?在Fiddler中出现的标题内容类型是'application / x-www-form-urlencoded'。

I am using Delphi 2010. The component doesn't have any special settings, I presume I might need to set something? The header content-type that comes through in Fiddler is 'application/x-www-form-urlencoded'.

然后,Delphi代码:

Then, the Delphi code:

Request:='hello world+dog';
URL    :='http://............./ExecuteWithErrors';

TSL:=TStringList.Create;
TSL.Add('query='+Request);

Try
begin
  IdHTTP1.ConnectTimeout:=5000;
  IdHTTP1.ReadTimeout   :=5000;

  Reply:=IdHTTP1.Post(URL,TSL);


推荐答案

您使用的是Indy的过时版本,需要升级。

You are using an outdated version of Indy and need to upgrade.

TIdHTTP 的webform数据编码器在2010年末多次更改。您的版本似乎早于所有版本这些更改。

TIdHTTP's webform data encoder was changed several times in late 2010. Your version appears to predate all of those changes.

在您的版本中, TIdHTTP 使用 TIdURI.ParamsEncode()在内部对表单数据进行编码,其中空格字符编码为%20 + 字符未被编码,因此:

In your version, TIdHTTP uses TIdURI.ParamsEncode() internally to encode the form data, where a space character is encoded as %20 and a + character is left un-encoded, thus:

hello%20world+dog

2010年10月,编码器更新后将空格字符编码为& ,然后再调用 TIdURI.ParamsEncode(),因此:

In October 2010, the encoder was updated to encode a space character as & before calling TIdURI.ParamsEncode(), thus:

hello&world+dog

2010年12月初,编码器更新为将空格字符编码为 + 相反,因此:

In early December 2010, the encoder was updated to encode a space character as + instead, thus:

hello+world+dog

2010年12月下旬,编码器完全重写以遵循W3C的HTML规范应用程序/ x-WWW窗体-urlencoded 。空格字符编码为 + + 字符编码为%2B ,因此:

In late December 2010, the encoder was completely re-written to follow W3C's HTML specifications for application/x-www-form-urlencoded. A space character is encoded as + and a + character is encoded as %2B, thus:

hello+world%2Bdog

在所有情况下,只有在<$ c中启用 hoForceEncodeParams 标志时才应用上述逻辑$ c> TIdHTTP.HTTPOptions 属性(默认情况下)。如果升级不是一个选项,则必须禁用 hoForceEncodeParams 标志并自行编码 TStringList 内容:

In all cases, the above logic is applied only if the hoForceEncodeParams flag is enabled in the TIdHTTP.HTTPOptions property (which it is by default). If upgrading is not an option, you will have to disable the hoForceEncodeParams flag and manually encode the TStringList content yourself:

Request:='hello+world%2Bdog';

这篇关于Delphi TIdHTTP POST不编码加号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆