delphi中URL的汉字编码 urlencode
    在线编码解码bianma.911cha/unicode://widestring('总经理2级')
//utf8
//\xE6\x80\xBB\xE7\xBB\x8F\xE7\x90\x86\x32\xE7\xBA\xA7
//urlencode
//%E6%80%BB%E7%BB%8F%E7%90%862%E7%BA%A7
HttpEncode(Utf8String('总经理2级'));ansifunction URLEncode(const S: ansistring; const InQueryString: Boolean): ansistring;
var
  Idx: Integer; // loops thru characters in string
begin
  Result := '';
  for Idx := 1 to Length(S) do
  begin
unicode在线工具    case S[Idx] of
      'A'..'Z', 'a'..'z', '0'..'9', '-', '_', '.':
        Result := Result + S[Idx];
      ' ':
        if InQueryString then
          Result := Result + '+'
        else
          Result := Result + '%20';
      else
        Result := Result + '%' + SysUtils.IntToHex(Ord(S[Idx]), 2);
    end;
  end;
end;
delphi中URL的汉字编码  urlencodedelphi中URL的汉字编码
show.asp?sort=全部&sortlevel=1&gorq=供&n=5&sitename=全部&img=yes&imgfile=/images/dot_g.gif
诸如这样的形式,在百度查询会转成GB2312的编码,每个汉字对应2个%xx%xx ,但是在google,每个汉字则对应的三个%xx%xx%xx,采用的是unicode编码
在delphi2010中,因为引入unicode的缘故,默认的成了3个%xx,导致我的程序出现问题,
了半天,每一个函数能够实现全URL的自动检测编码,所以自己写了一个,共享给大家:
uses
httpapp;
function urlencode(Aurl: string): string;
var
  i: integer;
  stmp,Tstmp: string;
begin
  result:=Aurl;
  if length(Aurl) > 0 then
  begin
    for i := 1 to length(Aurl) do
    begin
      if Integer(Ord(Aurl[i])) >255 then
        begin
        stmp := copy(Aurl, i, 1);
        Tstmp:=HttpEncode(stmp);
        result:=stringreplace(result,stmp,Tstmp,[]);
        end;
    end;
  end;
end;
使用:
URL :=urlencode(URL);
则URL中的汉字自动转为gb2312的%xx的编码。
参考:
ifso.iteye/blog/1513438