如果我使用具有Unicode代码页的HTML页面运行this code,则结果是乱码,因为在D7中TStringStream不是Unicode.页面可能是UTF8编码或其他(Ansi)代码页编码.
如何检测TStream / IPersistStreamInit是否为Unicode / UTF8 / Ansi?
我如何始终为此函数返回正确的WIDeString结果?
function GetWebbrowserHTML(const Webbrowser: TWebbrowser): WIDeString;
如果我用TMemoryStream替换TStringStream,并将TMemoryStream保存到文件中就可以了.它可以是Unicode / UTF8 / Ansi.但我总是希望以WIDeString的形式返回流:
function GetWebbrowserHTML(const Webbrowser: TWebbrowser): WIDeString;var // LStream: TStringStream; LStream: TMemoryStream; Stream : IStream; LPersistStreamInit : IPersistStreamInit;begin if not Assigned(Webbrowser.document) then exit; // LStream := TStringStream.Create(''); LStream := TMemoryStream.Create; try LPersistStreamInit := Webbrowser.document as IPersistStreamInit; Stream := TStreamAdapter.Create(LStream,soReference); LPersistStreamInit.Save(Stream,true); // result := LStream.DataString; LStream.Savetofile('c:\test\test.txt'); // test only - file is ok Result := ??? // WIDeString finally LStream.Free(); end;end;
编辑:我发现这篇文章 – How to load and save documents in TWebBrowser in a Delphi-like way
这完全符合我的需要.但它仅适用于Delphi Unicode编译器(D2009).阅读Conclusion部分:
There is obvIoUsly a lot more we Could do. A couple of things
immediately spring to mind. We retro-fit some of the Unicode
functionality and support for non-ANSI enCodings to the pre-Unicode
compiler code. The present code when compiled with anything earlIEr
than Delphi 2009 will not save document content to strings correctly
if the document character set is not ANSI.
魔术显然是在TEnCoding类(TEnCoding.GetBufferEnCoding)中.但是D7没有TEnCoding.有任何想法吗?
解决方法 我使用 GpTextStream来处理转换(应该适用于所有Delphi版本):function GetcodepageFromHTMLCharSet(Charset: WIDeString): Word;const WIN_CHARSET = 'windows-'; ISO_CHARSET = 'iso-';var S: string;begin Result := 0; if Charset = 'unicode' then Result := CP_UNICODE else if Charset = 'utf-8' then Result := CP_UTF8 else if Pos(WIN_CHARSET,Charset) <> 0 then begin S := copy(Charset,Length(WIN_CHARSET) + 1,Maxint); Result := StrToIntDef(S,0); end else if Pos(ISO_CHARSET,Charset) <> 0 then // ISO-8859 (e.g. iso-8859-1: => 28591) begin S := copy(Charset,Length(ISO_CHARSET) + 1,Maxint); S := copy(S,Pos('-',S) + 1,2); if S = '15' then // iso-8859-15 (Latin 9) Result := 28605 else Result := StrToIntDef('2859' + S,0); end;end;function GetWebbrowserHTML(Webbrowser: TWebbrowser): WIDeString;var LStream: TMemoryStream; Stream: IStream; LPersistStreamInit: IPersistStreamInit; TextStream: TGpTextStream; Charset: WIDeString; Buf: WIDeString; codepage: Word; N: Integer;begin Result := ''; if not Assigned(Webbrowser.document) then Exit; LStream := TMemoryStream.Create; try LPersistStreamInit := Webbrowser.document as IPersistStreamInit; Stream := TStreamAdapter.Create(LStream,soReference); if Failed(LPersistStreamInit.Save(Stream,True)) then Exit; Charset := (Webbrowser.document as IHTMLdocument2).charset; codepage := GetcodepageFromHTMLCharSet(Charset); N := LStream.Size; SetLength(Buf,N); TextStream := TGpTextStream.Create(LStream,tsaccRead,[],codepage); try N := TextStream.Read(Buf[1],N * SizeOf(WIDeChar)) div SizeOf(WIDeChar); SetLength(Buf,N); Result := Buf; finally TextStream.Free; end; finally LStream.Free(); end;end;总结
以上是内存溢出为你收集整理的delphi – 来自TWebBrowser的HTML源代码 – 如何检测流编码?全部内容,希望文章能够帮你解决delphi – 来自TWebBrowser的HTML源代码 – 如何检测流编码?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)