delphi 获得网页乱码


Delphi7里idhttp(UTF-8网页)乱码的话 可以用UTF8Decode()函数解决
例如:Memo1.text:=UTF8Decode(idhttp1.get(**));
但到了delphi2010里在用UTF8Decode这个函数转换就不行 还是会乱码
所以我们可以这样
procedure TForm1.SpeedButton1Click(Sender: TObject);
Var
cText:TstringStream; //流
idHttp1:TIDHTTP;
begin
cText:=TStringStream.Create(**,TEncoding.UTF8); //指定创建流的编码格式TEncoding这个单元从2009版本开始才有
idHttp1:=TIDHTTP.Create(nil);
idHttp1.Get(*http://quanben.qidian.com/Default.aspx*,cText);
idHttp1.Free;
Memo1.Text:=cText.DataString;
cText.Free;
end;


//以下是自动识别UTF-8和GBK,GB2312的页面并返回HTML代码


function GET_HTMLInt(const URL:string):Integer ; // idhttp控件
var
HTTP:TIdHTTP;
HTML:string;
GBK_2312:TStringStream;
UTF8Str:TStringStream;
UTF8HTML:UTF8String;
begin
try
HTTP:=TIdHTTP.Create(nil);
UTF8Str:=TStringStream.Create(**,TEncoding.UTF8);
GBK_2312:=TStringStream.Create(**,TEncoding.Default);
HTTP.Get(URL,GBK_2312);
HTML:=GBK_2312.DataString;
if Pos(*utf-8*,HTML) > 0 then //查找UTF8字符串
begin
HTTP.Get(URL,UTF8Str);
UTF8HTML:=UTF8Str.DataString;
Result:=Length(UTF8HTML);
Exit;
end;
Result:=Length(HTML);
finally
GBK_2312.Free;
UTF8Str.Free;
HTTP.Free; /
end;
end;
//=============================================================================
function GET_HTTP(const URL :string):Integer; // ICS控件实现
Var
HTTP:THttpCli;
HTMLCode:String;
UTF8HTML:UTF8String;
CodeStr:TStringStream;
UTF8Str:TStringStream;
begin
HTTP:=THttpCli.Create(nil);
HTTP.Agent:= *Mozilla/5.0 (compatible; ICS)*;
HTTP.Accept:=*text/html,application/xhtml+xml,application/xml,*/** ;
HTTP.Timeout:=30;
CodeStr:=TStringStream.Create(**,TEncoding.Default);
UTF8Str:=TStringStream.Create(**,TEncoding.UTF8);
HTTP.RcvdStream:=CodeStr;
HTTP.URL:=Trim(URL);
HTTP.Get;
HTMLCode:=CodeStr.DataString;
if AnsiPos(*utf-8*,HTMLCode) > 0 then
begin
HTTP.Abort;
HTTP.RcvdStream:=UTF8Str; //UTF8
HTTP.URL:=Trim(URL);
HTTP.Get;
UTF8HTML:=UTF8Str.DataString;
Result:=Length(UTF8HTML);
UTF8Str.Free;
CodeStr.Free;
HTTP.Free;
Exit;
end;
Result:=Length(HTMLCode);
UTF8Str.Free;
CodeStr.Free;
HTTP.Free;
end;
//=============================================================================
function GET_HTML (const URL : string):string; // XMLHTTP接口
Var
XMLHTTP:IXMLHTTPRequest;
HTML:TBytes;
begin
try
CoInitializeEx(nil,COINIT_MULTITHREADED);
XMLHTTP:=CoXMLHTTPRequest.Create;
XMLHTTP.open(*GET*,URL,False,EmptyParam,EmptyParam);
XMLHTTP.send(EmptyParam);
HTML:=XMLHTTP.responseBody;
if Pos(*utf-8*,StringOf(HTML)) > 0 then
begin
Result:=TEncoding.Default.GetEncoding(65001).GetString(HTML);//把UTF8转STRING
Exit;
end;
Result:=TEncoding.Default.GetString(HTML);
finally
CoUnInitialize; // 释放内存
end;
end;

联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678