M
macrolen
Unregistered / Unconfirmed
GUEST, unregistred user!
只是在Delphi2010下这样,D7下没问题。中文:“明信片”,UTF8编码后正确的应该是:%E6%98%8E%E4%BF%A1%E7%89%87 可是用下面的代码得到的结果是:%E6%98%8E%E4%BF%A1%E7%89%00怎么解决这个问题呢? unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function URLEncode(const S: AnsiString; const InQueryString: Boolean): AnsiString; var Idx: Integer; begin Result := ''; for Idx := 1 to Length(S) do begin 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; function ParamList():AnsiString; var str : AnsiString; i : Integer; List : TStringList; begin Result := ''; List := TStringList.Create; try List.Add('q=' + UTF8Encode('明信片')); List.Add('cid='); List.Add('nicks='); List.Add('price='); List.Add('page='); List.Add('size='); List.Add('orderby='); List.Add('status='); List.Add('post='); List.Add('v=1.0'); List.Sorted := True; //所有参数以字母升序排列 for i := 0 to List.Count-1 do begin str := str + List.Strings + '&'; end; Result := str; finally List.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Text := URLEncode(ParamList(),true); end; end.