Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:43:48+00:00 2026-06-17T07:43:48+00:00

Based on this question: How can I get HTML source code from TWebBrowser If

  • 0

Based on this question: How can I get HTML source code from TWebBrowser

If I run this code with a html page that has Unicode code page, the result is gibberish becouse TStringStream is not Unicode in D7. the page might be UTF8 encoded or other (Ansi) code page encoded.

How can I detect if a TStream/IPersistStreamInit is Unicode/UTF8/Ansi?

How do I always return correct result as WideString for this function?

function GetWebBrowserHTML(const WebBrowser: TWebBrowser): WideString;

If I replace TStringStream with TMemoryStream, and save TMemoryStream to file it’s all good. It can be either Unicode/UTF8/Ansi. but I always want to return the stream back as 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;

EDIT: I found this article – How to load and save documents in TWebBrowser in a Delphi-like way

Which does exactlly what I need. but it works correctlly only with Delphi Unicode compilers (D2009+). read Conclusion section:

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.

The magic is obviously in TEncoding class (TEncoding.GetBufferEncoding). but D7 does not have TEncoding. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:43:50+00:00Added an answer on June 17, 2026 at 7:43 am

    I used GpTextStream to handle the convertion (Should work for all Delphi versions):

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on some other code I found from this site (This Question here -
This is a further question based on this answer: How can I implement a
This question is based on this thread . The code function man() { man
I have this code to get results based in search query: $(document).ready(function(){ $('#envio').click(function(){ var
Based on this question I've created a small application which is catching all debug
Based on this question I understand the purpose of the construct in linking C
Based on this question How to insert array into mysql using PDO and bindParam?
This question is based on this thread . Is [---] a comment in Git
This question is based on this thread . I am interested in how Git
This question is based on this thread in Meta . I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.