delphi xe2 IdHTTP1出现乱码,怎么解决?
在delphi 7下能执行的一段代码
Memo1.Text:=IdHTTP1.Get('http://www.ip138.com/ip2city.asp');
到xe2下 中文就出现乱码了,如何解决?
这是获取本机公网ip的功能,在xe2下还有没有更好的获取办法?
[解决办法]
IP138返回的网页编码是GB2312 ,返回汉字要强制指定编码
procedure TForm1.Button1Click(Sender: TObject);
var
ss: TStringStream;
begin
IdHTTP1.Request.UserAgent :=
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
SELF.IdHTTP1.ProtocolVersion:= pv1_0;
ss := TStringStream.Create('', TEncoding.GetEncoding(936));
try
idhttp1.Get('http://city.ip138.com/city.asp', ss);
memo1.Text := ss.DataString;
finally
ss.Free;
end;
END;