Delphi TIdHTTP POST does not encode plus sign

孤者浪人 提交于 2019-12-11 03:18:21

问题


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'.

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 '+'.

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.

Other information:

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'.

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);

回答1:


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

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

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

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

hello&world+dog

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

hello+world+dog

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

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';


来源:https://stackoverflow.com/questions/32461631/delphi-tidhttp-post-does-not-encode-plus-sign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!