网页表单提交时的字符编码问题:
例如在Google中,如果你查询:“中国”,两个字,当出现结果时浏览器的地址栏中出现:“http://www.google.com/search?q=%E4%B8%AD%E5%9B%BD&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=lang_zh-CN”这样的网址信息,在这里,浏览器是把“中国”汉字转换成了:“%E4%B8%AD%E5%9B%”字符,他是怎么转的?“%E4%B8%AD%E5%9B%”是UTF8编码吗?还是其他的编码?我转了好长时间,怎么转不对呢?我现在想模拟Google的提交,比如:在我的窗体中有一个Edit1:TEdit,有一个WebBrower1:TWebBrower,在Edit1中输入要查询的关键字,然后URL:=‘http://www.google.com/search?q=’+Edit1.Text+‘&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=lang_zh-CN’;用WebBrower1来获得这个网页,我怎样来转换Edit1.Text中的中文字符呢?
例如在Google中,如果你查询:“中国”,两个字,当出现结果时浏览器的地址栏中出现:“http://www.google.com/search?q=%E4%B8%AD%E5%9B%BD&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=lang_zh-CN”这样的网址信息,在这里,浏览器是把“中国”汉字转换成了:“%E4%B8%AD%E5%9B%”字符,他是怎么转的?“%E4%B8%AD%E5%9B%”是UTF8编码吗?还是其他的编码?我转了好长时间,怎么转不对呢?我现在想模拟Google的提交,比如:在我的窗体中有一个Edit1:TEdit,有一个WebBrower1:TWebBrower,在Edit1中输入要查询的关键字,然后URL:=‘http://www.google.com/search?q=’+Edit1.Text+‘&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=lang_zh-CN’;用WebBrower1来获得这个网页,我怎样来转换Edit1.Text中的中文字符呢?
是不是十六进制呢?
unit DzURL;{ By Alexander Dzyubenko
[email protected]
http://www.dzsoft.com }interfaceuses SysUtils;function UrlEncode(const DecodedStr: String; Pluses: Boolean): String;
// Encodes standard string into URL data format.
// Example: http://www.dzsoft.com -> http%3A%2F%2Fwww.dzsoft.com%2F
// Pluses parameter specifies whether spaces will be
// encoded as '+' or as '%20'function UrlDecode(const EncodedStr: String): String;
// Decodes URL data into a readable string.
// Example: http%3A%2F%2Fwww.dzsoft.com%2F -> http://www.dzsoft.comfunction HexToInt(HexStr: String): Int64;
// Taken from http://www.delphi3000.com/article.asp?id=1412implementationfunction UrlEncode(const DecodedStr: String; Pluses: Boolean): String;
var
I: Integer;
begin
Result := '';
if Length(DecodedStr) > 0 then
for I := 1 to Length(DecodedStr) do
begin
if not (DecodedStr[I] in ['0'..'9', 'a'..'z',
'A'..'Z', ' ']) then
Result := Result + '%' + IntToHex(Ord(DecodedStr[I]), 2)
else if not (DecodedStr[I] = ' ') then
Result := Result + DecodedStr[I]
else
begin
if not Pluses then
Result := Result + '%20'
else
Result := Result + '+';
end;
end;
end;function UrlDecode(const EncodedStr: String): String;
var
I: Integer;
begin
Result := '';
if Length(EncodedStr) > 0 then
begin
I := 1;
while I <= Length(EncodedStr) do
begin
if EncodedStr[I] = '%' then
begin
Result := Result + Chr(HexToInt(EncodedStr[I+1]
+ EncodedStr[I+2]));
I := Succ(Succ(I));
end
else if EncodedStr[I] = '+' then
Result := Result + ' '
else
Result := Result + EncodedStr[I]; I := Succ(I);
end;
end;
end;function HexToInt(HexStr: String): Int64;
var RetVar : Int64;
i : byte;
begin
HexStr := UpperCase(HexStr);
if HexStr[length(HexStr)] = 'H' then
Delete(HexStr,length(HexStr),1);
RetVar := 0; for i := 1 to length(HexStr) do begin
RetVar := RetVar shl 4;
if HexStr[i] in ['0'..'9'] then
RetVar := RetVar + (byte(HexStr[i]) - 48)
else
if HexStr[i] in ['A'..'F'] then
RetVar := RetVar + (byte(HexStr[i]) - 55)
else begin
Retvar := 0;
break;
end;
end; Result := RetVar;
end;end.
牛人!大哥,你是不是姓牛啊,以后你就是我的牛大哥了啊。哈哈,我已经按照你写的函数转换成功了啊。我这个帖子总分是100分,我想给你95分,给一楼的大哥5分,你不会介意吧。谢谢各位的捧场,再次谢谢aiirii(ari-爱的眼睛),同时也谢谢一楼的iwhp(无拘无束 for java) 。开开心心结贴拉啊。