Edit2.Text :=Tnetencoding.url.EncodeBytesToString( Tencoding.GetEncoding(936).getbytes('提交'));//会提示错误No mapping for the Unicode character exists in the target multi-byte code page.
function MyUrlEncode(const input: string): string; // 汉字转Gb2312 urlencode
var
S: string;
Stream: TStringStream;
B: Byte;
begin
Result := '';
S := '';
try
Stream := TStringStream.Create(input, TEncoding.GetEncoding(936));
for B in Stream.Bytes do
S := Format('%s%%%.2x', [S, B]);
finally
Stream.Free;
end;
Result := S;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := '提交';
Edit2.Text := MyUrlEncode(Edit1.Text); // 结果 %cc%e1%bd%bb
end;