近期在做网址编码相关的工作,发现在用 XE5 编译的时候,一切正常,拿 到 XE7下 就 结果错误了。百度了下,谷歌 了下,有人提出,但是,我没有找到答案,也许都没有碰到这个问题,也许都己经自己默默的解决了,在此 小记一下,方便后人,也方便自己 查寻。

例子 : 原字符   "过年"

httpencode('过年') 结果 :

XE5为  %B9%FD%C4%EA

XE7 调用 相当函数结果 %E8%BF%87%E5%B9%B4

百思不得其解啊,折腾了很长时间,后来终于想到是不是 此函数 官方更新修改了,(没办法,人比较笨)

于是查看源码:

XE5的 web.httpapp 中(分string 和 ansistring, 我用ansistring 版本得到 期望的结果):

 function HTTPEncode(const AStr: string): string;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = [Ord('A')..Ord('Z'), Ord('a')..Ord('z'), Ord('*'), Ord('@'),
Ord('.'), Ord('_'), Ord('-'), Ord('')..Ord(''), Ord('$'),
Ord('!'), Ord(''''), Ord('('), Ord(')')];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * );
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <> # do
begin
if Ord(Sp^) in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp, , string('%%%.2x'), , [Ord(Sp^)]);
Inc(Rp,);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end; function HTTPDecode(const AStr: string): string;
var
Sp, Rp, Cp: PChar;
S: string;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <> # do
begin
case Sp^ of
'+': Rp^ := ' ';
'%': begin
// Look for an escaped % (%%) or %<hex> encoded character
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <> #) and (Sp^ <> #) then
begin
S := Char('$') + Cp^ + Sp^;
Rp^ := Char(StrToInt(string(S)));
end
else
raise EWebBrokerException.CreateFmt(sErrorDecodingURLText, [Cp - PChar(AStr)]);
end;
end;
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidURLEncodedChar,
[Char('%') + Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end; function HTTPEncode(const AStr: AnsiString): AnsiString;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
''..'','$','!','''','(',')'];
var
Sp, Rp: PAnsiChar;
begin
SetLength(Result, Length(AStr) * );
Sp := PAnsiChar(AStr);
Rp := PAnsiChar(Result);
while Sp^ <> # do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
System.AnsiStrings.FormatBuf(Rp^, , AnsiString('%%%.2x'), , [Ord(Sp^)]);
Inc(Rp,);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PAnsiChar(Result));
end;

在XE7中

web.httpapp:

function HTTPEncode(const AStr: string): string;
begin
Result := TNetEncoding.URL.Encode(AStr);
end;

查看 system.netencoding

找到

function TNetEncoding.DoEncode(const Input: array of Byte): TBytes;
begin
Result := TEncoding.UTF8.GetBytes(DoEncode(TEncoding.UTF8.GetString(@Input[])));
end;
查看类定义:  
TURLEncoding = class(TNetEncoding)
protected
function DoDecode(const Input: string): string; overload; override;
function DoEncode(const Input: string): string; overload; override;
end;
查看函数代码:
function TURLEncoding.DoEncode(const Input: string): string;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = [Ord('A')..Ord('Z'), Ord('a')..Ord('z'), Ord('*'), Ord('@'),
Ord('.'), Ord('_'), Ord('-'), Ord('')..Ord(''), Ord('$'),
Ord('!'), Ord(''''), Ord('('), Ord(')')]; procedure AppendByte(B: Byte; var Buffer: PChar);
const
Hex = '0123456789ABCDEF';
begin
Buffer[] := '%';
Buffer[] := Hex[B shr + Low(string)];
Buffer[] := Hex[B and $F + Low(string)];
Inc(Buffer, );
end; var
Sp, Rp: PChar;
MultibyteChar: TBytes;
I, ByteCount: Integer;
begin
// Characters that require more than 1 byte are translated as "percent-encoded byte"
// which will be encoded with 3 chars per byte -> %XX
// Example: ?character
// Multibyte representation: C391 (2 bytes)
// URL encode representation: %C3%91
//
// So the worst case is 4 bytes(max) per Char, and 3 characters to represent each byte
SetLength(Result, Length(Input) * * );
Sp := PChar(Input);
Rp := PChar(Result);
SetLength(MultibyteChar, );
while Sp^ <> # do
begin
if Ord(Sp^) in NoConversion then
begin
Rp^ := Sp^;
Inc(Rp)
end
else if Sp^ = ' ' then
begin
Rp^ := '+';
Inc(Rp)
end
else
begin
if (Ord(Sp^) < ) then
// Single byte char
AppendByte(Ord(Sp^), Rp)
else
begin
// Multi byte char
ByteCount := TEncoding.UTF8.GetBytes([Sp^], , , MultibyteChar, );
for I := to ByteCount - do
AppendByte(MultibyteChar[I], Rp);
end
end;
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

似乎有点不同。

目前是我自己建立一个函数 ,复制XE5版本的 代码 放在XE7里面调用,得到希望的结果的。

代码:

function MyHTTPEncode(const AStr: AnsiString): AnsiString;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
''..'','$','!','''','(',')'];
var
Sp, Rp: PAnsiChar;
begin
SetLength(Result, Length(AStr) * );
Sp := PAnsiChar(AStr);
Rp := PAnsiChar(Result);
while Sp^ <> # do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
System.AnsiStrings.FormatBuf(Rp^, , AnsiString('%%%.2x'), , [Ord(Sp^)]);
Inc(Rp,);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PAnsiChar(Result));
end;

最新文章

  1. Json.Net 数据解析
  2. homework160809207刘兆轩
  3. 如何写出小而清晰的函数?(JS 版)
  4. js关闭页面(兼容浏览器)
  5. 水晶報表中小寫變大寫的函數-VB
  6. Python之路第十二天,高级(6)-paramiko
  7. Android——仿QQ聊天撒花特效
  8. 产品专家Marty Cagan:不做仅仅会编码的人
  9. NSAttributedString in Swift
  10. Dapper入门教程(三)——Dapper Query查询
  11. Interesting卡常数
  12. 系统运行缓慢,CPU 100%,以及Full GC次数过多问题的排查思路
  13. 侯哥的Python分享
  14. 微信小程序实战[01]
  15. 浅谈static关键字的四种用法
  16. Elasticsearch学习笔记(三)聚合分析Agg
  17. DOM事件监听器
  18. VRRP、Track与NQA联动配置举例(Master监视上行链路)
  19. Ing_制作在线QQ
  20. 怎么删除Elasticsearch里的index内容

热门文章

  1. win7 32位用pyinstaller打包Python和相关html文件 成exe
  2. mac 配置pylucene
  3. 正确理解hadoop 2.x 的环形缓冲区: (一) MR环形缓冲区的结构
  4. 2017.2.21 activiti实战--第十三章--流量数据查询与跟踪(一)查询接口介绍及运行时数据查询
  5. (转)微信小程序开发项目——笑话大全
  6. crm操作业务部门
  7. 4pda.ru注冊验证的解码算法
  8. MySQL -进阶
  9. Struts2学习三----------Action搜索顺序
  10. 通配符的匹配很全面, 但无法找到元素 &#39;context:component-scan&#39; 的声明。