Search code examples
delphidelphi-xe8

Delphi XE8: HTTPEncode convert string error


I use the HTTPEncode() function in Delphi XE8 to encode Japanese text. Some characters can encode correctly, but some cannot. Below is an example:

aStr := HTTPEncode('萩原小学校');

I expected this:

aStr = '%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1'

But I got this:

aStr = '%E8%90%A9%E5%8E%9F%E5%B0%8F%3F%E6%A0%A1'

Can someone help me to encode '萩原小学校' as '%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1'?


Solution

  • I'm not sure what this HTTPEncode function is. There is a function in Web.HTTPApp of that name. Perhaps that is what you refer to. If so, it is clearly marked as deprecated. Assuming you have enabled compiler warnings, the compiler will be telling you this, and telling you also to use TNetEncoding.UTL.Encode instead.

    Let's try that:

    {$APPTYPE CONSOLE}
    uses
      System.NetEncoding;
    
    begin
      Writeln(TNetEncoding.URL.Encode('萩原小学校'));
    end.
    

    Output

    %E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1