0

How to put the source code of a website in memo1 when it is compressed with gzip.

I already used this metod to obtain the source:

function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
  var
  LStream: TStringStream;
  Stream : IStream;
  LPersistStreamInit : IPersistStreamInit;
begin
  if not Assigned(WebBrowser.Document) then exit;
  LStream := TStringStream.Create('');
  try
    LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
    Stream := TStreamAdapter.Create(LStream,soReference);
    LPersistStreamInit.Save(Stream,true);
    result := LStream.DataString;
  finally
    LStream.Free();
  end;
end;

But when I trying to use this method on the compressed site I receives an empty result or few randome characters.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
Tazman666
  • 1
  • 1

2 Answers2

1

The HTML is uncompressed when it is downloaded from the server, before it can be displayed to the user. As such, the DOM interfaces will never return compressed HTML to you, so there is no need to worry about this at all. The code you showed will work just fine.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • So is it any possibility that page is encrypted? The above code work fine for almost every page but not for one i want. The result is empty, so i thought it must be gzip compression – Tazman666 Feb 03 '14 at 22:39
  • Encryption and compression are only applied during transport, not during parsing and displaying. So no, you will NEVER see encrypted/compressed data come out of the DOM interfaces. If you are trying to access some piece of data and it is coming back empty, either you are not accessing it correctly to begin with, or it really is empty. – Remy Lebeau Feb 03 '14 at 22:43
0

Firstly, many thanks for Remy Lebeau for your help, your suggestions really helped me to solve the problem.

In summary i could't find the reason why the above code does not work for me on one page but I found an alternative that works.

uses MSHTML

procedure TForm1.Button1Click(Sender: TObject);
begin
 Memo1.Text:=(WebBrowser1.Document AS IHTMLDocument2).body.innerHTML;
end;

I don't know what restrictions have the above solution but until now works graet.

Tazman666
  • 1
  • 1