Link to home
Start Free TrialLog in
Avatar of delphi3
delphi3

asked on

Stream html directly into Netscape or IE

Hi all,
I have D4 and Win2K with NT. I use ICS and want to stream a xxxx.html  directly into netscape.

My present practice is to stream the html file onto  the drive first saving it , then launch netscape and then load the html from the drive into netscape.

If it is possible an example  would be appreciated.

Delphi3

Avatar of edey
edey

I have no idea how to do it with Netscape, but for ie:

Q: How do I load a stream into the WebBrowser without Navigating to a file?

A: Here's some sample code:

function TForm1.LoadFromStream(const AStream: TStream): HRESULT;
begin
  AStream.seek(0, 0);
  Result := (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(AStream));
end;

I found this tip here: http://members.home.net/hfournier/

GL
Mike
Avatar of delphi3

ASKER

Mike,
It is having a difficult time with

IPersistStreamInit
Any suggestions?

and the address listed above has no DNS entry
Delphi3
oooh, sorry, try .com instead of .net . As far as the interface, what ver of delphi are you useing? perhaps it maybe related to that.

GL
Mike
Avatar of delphi3

ASKER

Mike,
Let's skip Netscape for the moment.

Answering your Q:

I have D4 standard edition and Win2K with NT

Delphi3


Oooh, I guess I prob. should have mentioned that the interface is declared in axcitveX.pas - so you'll need to add activeX to your uses clause.  Sorry about that.  Also, because the code refrences a webBrowser's document property you'll need to make sure that it has one.  When you first drop the control on a form it doesn't - until it gets 'navigated' somewhere.  I like to do somehting like this:

procedure TForm1.FormCreate(Sender: TObject);
begin
     webBrowser1.navigate('about:blank');
end;


GL
Mike
Avatar of delphi3

ASKER

Mike,
This is going no where.
your hints or snips are too vague and incomplete. Or I am too stuupid to make it all happen.

And the prompts and programming from D4  help are of no use when using this activeX. I tried to use
IE instead of webbrowser so that it read

       function TForm1.LoadFromStream(const AStream: TStream): HRESULT;
                 begin
                  AStream.seek(0, 0);
                  Result := (IE.Document as IPersistStreamInit).Load(TStreamAdapter.Create(AStream));
                 end;
and even with the change not being made  to IE, and keeping the WebBrowser1 it still does not know
what to do with the   IPersistStreamInit.

Please if you have  no more help say so and I will put the Q out to pasture: close up and put the Q to electronic
heaven or into the recycle bin

Delphi3
We'll try an example.  This assumes you've got a for with a button, a memo & an embeded IE ActiveX called webBrowser1.  If you've got the netmasters components install your IE might be called something else - adjust as nec.  The project should take html entered in the memo & render it in the webBrowser:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,activeX,
  StdCtrls, OleCtrls, SHDocVw_TLB;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
   ms : TMemoryStream;
begin
     webBrowser1.navigate('about:blank');
     while webBrowser1.busy do
           application.processMessages;
     ms := TMemoryStream.create;
     memo1.lines.saveToStream(ms);
     ms.seek(0, 0);
     (webBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
     ms.free;
end;

end.


This code was successfully compiled & tested with D5 Pro, srv pack 1.

GL
Mike
Avatar of delphi3

ASKER

Mike,
I would want to report that I had success all the way.
Since my version is D4  standard edition, I am going no where fast.

I do see that you load from a memo, and I was looking to load from a stream. So all in all
this is a no go anywhere situation.
For now I will let this sit here a while unless you have any  more bright ideas.


Delphi3

 
ASKER CERTIFIED SOLUTION
Avatar of edey
edey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of delphi3

ASKER

Mike,

No,  Actually I have a strange set of things. First, my D4 TWebBrowser was obtained  is on the palet by
install instructions by looking at the import ActiveX, selecting Microsoft internet controls version 1.1 and
what results is a TWebBrowser_V1.


Here is what I have:

unit GetWebBrowserUnit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, activeX,
  StdCtrls, OleCtrls, SHDocVw_TLB;

type
  TForm1 = class(TForm)
    //WebBrowser1: TWebBrowser;
    Memo1: TMemo;
    Button1: TButton;
    WebBrowser_V11: TWebBrowser_V1;
    procedure Button1Click(Sender: TObject);
  private
                    { Private declarations }
  public
                    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
 V:OleVariant;
  ms: TMemoryStream;
begin
  webBrowser_V11.navigate('about:blank',V,V,V,V); // had to add the V.V.V.V this to keep it happy
  while webBrowser_V11.busy do
    application.processMessages;
  ms := TMemoryStream.create;
  memo1.lines.saveToStream(ms);
  ms.seek(0, 0);
  (webBrowser_V11.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
  ms.free;
end;
end.

There is no way that I can get it to accept the form. I can't get D4 to open it after once I have
failure to run it. A bunch of violations result.

on the other hand I have this thing that runs well  and uses IE, puts the HTML
text into the memo window and for which I want to stream it.

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Author:       Fran?ois PIETTE
Creation:     January 17, 1998      notes: removed

 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit HttpGet1;

interface

uses
  WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, HttpProt, StdCtrls, IniFiles, ExtCtrls, ComObj, ComCtrls, OleCtrls;
  // Added   ComObj, ComCtrls, OleCtrls to the original  list of uses
type
  THttpGetForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    InfoLabel: TLabel;
    URLEdit: TEdit;
    ProxyHostEdit: TEdit;
    ProxyPortEdit: TEdit;
    FileNameEdit: TEdit;
    GetButton: TButton;
    AbortButton: TButton;
    Button1: TButton;
    Button2: TButton;
    HttpCli1: THttpCli;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    RadioGroup1: TRadioGroup;
    Memo1: TMemo;
    procedure GetButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure HttpCli1DocData(Sender: TObject; Buffer: Pointer;
      Len: Integer);
    procedure HttpCli1HeaderData(Sender: TObject);
    procedure AbortButtonClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FInitialized: Boolean;
    FIniFileName: string;
  public
  end;

var
  HttpGetForm: THttpGetForm;

implementation

{$R *.DFM}
const
  SectionData = 'Data';
  KeyURL = 'URL';
  KeyProxyHost = 'ProxyHost';
  KeyProxyPort = 'ProxyPort';
  KeyFileName = 'FileName';
  SectionWindow = 'Window';
  KeyTop = 'Top';
  KeyLeft = 'Left';
  KeyWidth = 'Width';
  KeyHeight = 'Height';


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.FormCreate(Sender: TObject);
begin
  FIniFileName := LowerCase(ExtractFileName(Application.ExeName));
  FIniFileName := Copy(FIniFileName, 1, Length(FIniFileName) - 3) + 'ini';
  InfoLabel.Caption := '';
  RadioGroup1.ItemIndex := 0;
  Memo1.Clear;

end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.FormShow(Sender: TObject);
var
  IniFile: TIniFile;
begin
  //Statement placed here to prepare to Coinitialize
  if InitProc <> nil then TProcedure(InitProc);
  //*************
  if not FInitialized then begin
    FInitialized := TRUE;
    IniFile := TIniFile.Create(FIniFileName);
    URLEdit.Text := IniFile.ReadString(SectionData, KeyURL,
      'http://www.rtfm.be/fpiette/images/overbyte.gif');
    ProxyHostEdit.Text := IniFile.ReadString(SectionData, KeyProxyHost,
      '');
    ProxyPortEdit.Text := IniFile.ReadString(SectionData, KeyProxyPort,
      '80');
    FileNameEdit.Text := IniFile.ReadString(SectionData, KeyFileName,
      'test.tmp');
    Top := IniFile.ReadInteger(SectionWindow, KeyTop, Top);
    Left := IniFile.ReadInteger(SectionWindow, KeyLeft, Left);
    Width := IniFile.ReadInteger(SectionWindow, KeyWidth, Width);
    Height := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);

    IniFile.Free;
  end;
  RadioGroup1.ItemIndex := 0;
  URLEdit.Text := 'http://users.swing.be/francois.piette/wsocketu.htm';
  FileNameEdit.Text := 'View1.html';

end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.RadioGroup1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex = 0 then begin //ICS
    URLEdit.Text := 'http://users.swing.be/francois.piette/wsocketu.htm';
    FileNameEdit.Text := 'View1.html';
  end;

  if RadioGroup1.ItemIndex = 1 then begin //Delphi Tips
    URLEdit.Text := 'https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20161215&jsessionid=200156996586977326'; // EE Q
    FileNameEdit.Text := 'View2.html';
  end;

end;

procedure THttpGetForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create(FIniFileName);
  IniFile.WriteString(SectionData, KeyURL, URLEdit.Text);
  IniFile.WriteString(SectionData, KeyProxyHost, ProxyHostEdit.Text);
  IniFile.WriteString(SectionData, KeyProxyPort, ProxyPortEdit.Text);
  IniFile.WriteString(SectionData, KeyFileName, FileNameEdit.Text);
  IniFile.WriteInteger(SectionWindow, KeyTop, Top);
  IniFile.WriteInteger(SectionWindow, KeyLeft, Left);
  IniFile.WriteInteger(SectionWindow, KeyWidth, Width);
  IniFile.WriteInteger(SectionWindow, KeyHeight, Height);
  IniFile.Free;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.GetButtonClick(Sender: TObject);
var
  S: string;
begin

  HttpCli1.URL := URLEdit.Text;
  HttpCli1.Proxy := ProxyHostEdit.Text;
  HttpCli1.ProxyPort := ProxyPortEdit.Text;
  HttpCli1.RcvdStream := TFileStream.Create(FileNameEdit.Text, fmCreate);
  GetButton.Enabled := FALSE;
  AbortButton.Enabled := TRUE;
  InfoLabel.Caption := 'Loading';
  try
    try
      HttpCli1.Get;
      InfoLabel.Caption := 'Received ' +
        IntToStr(HttpCli1.RcvdStream.Size) + ' bytes';
    except
      on E: EHttpException do begin
        InfoLabel.Caption := 'Failed : ' +
          IntToStr(HttpCli1.StatusCode) + ' ' +
          HttpCli1.ReasonPhrase; ;
      end
    else
      raise;
    end;
  finally

    GetButton.Enabled := TRUE;
    AbortButton.Enabled := FALSE;

    HttpCli1.RcvdStream.Seek(0, soFromBeginning);
    SetLength(S, HttpCli1.RcvdStream.Size);
    HttpCli1.RcvdStream.Read(S[1], length(S));
    Memo1.Text := S;

    HttpCli1.RcvdStream.Destroy;
    HttpCli1.RcvdStream := nil;
    InfoLabel.Caption := 'Task Completed';
  end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.HttpCli1DocData(Sender: TObject; Buffer: Pointer;
  Len: Integer);
begin
  InfoLabel.Caption := IntToStr(HttpCli1.RcvdCount);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.HttpCli1HeaderData(Sender: TObject);
begin
  InfoLabel.Caption := InfoLabel.Caption + '.';
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.AbortButtonClick(Sender: TObject);
begin
  HttpCli1.Abort;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.Button1Click(Sender: TObject);
var
  IE, Doc, Bod, V: oleVariant;
  I, I1, X: integer;
  S: string;
  F: TStream;
begin
  Memo1.Clear;
  F := TFileStream.Create(ExtractFilePath(Application.exeName) + FileNameEdit.Text, fmOpenRead or fmShareDenyNone);
  try
    SetLength(S, F.Size);
    F.Read(S[1], Length(S));
  finally
    F.Free;
    Memo1.lines.add(s);
  end;
  IE := CreateOleObject('InternetExplorer.Application');
  IE.Navigate('about:blank', V, V, V, V);
  IE.Visible := true; //false keeps it hidden
  while IE.Busy do
    Application.ProcessMessages;

  Doc := IE.Document;
  Bod := Doc.Body;
  Bod.InnerHTML := Memo1.Text;
  Memo1.Text := Bod.InnerText;
  //IE.Quit;

  InfoLabel.Caption := 'Saving Text File';
  with TFileStream.Create(ExtractFilePath(Application.exeName) + 'TextSavedSeenInView' + FileNameEdit.Text[5] + '.txt', fmCreate) do

  try
    I := 0;
    X := Memo1.Lines.Count;
    while I < X do
    begin
      Memo1.Lines[I];
      I1 := Length(Memo1.Lines[I]);
      Write(Pointer(Memo1.Lines[I])^, I1);
      Inc(I);
    end;
  finally free end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

procedure THttpGetForm.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.





Avatar of delphi3

ASKER

Mike,
Sorry,  but I just don't see how you are going from the internet download stream to stream into IE without going first
to a download on the disk..
Anyway , you tried = 'A' grade and  50 points.

Delphi3
Ok, perhaps we need to restate the question.  In my example I filled the webBrowser with data from a TMemoryStream, that's a delphi object that gives abstracted access to a chunk of memory like it was a file.  If I understand your last comment correctly you said you wanted to grab a file off some remote internet site & display it in the webBrowser without saving it to disk first.  If I understand right, why not something like:

webBrowser1.navigate('http://www.experts-exchange.com');

?

GL
Mike
Avatar of delphi3

ASKER

Mike,

Super!! , Well done!!  Thanks.

unit GetWebBrowserUnit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, activeX,
  StdCtrls, OleCtrls, SHDocVw_TLB;

type
  TForm1 = class(TForm)
    //WebBrowser1: TWebBrowser;
    Memo1: TMemo;
    Button1: TButton;
    WebBrowser_V11: TWebBrowser_V1;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    URL: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
                    { Private declarations }
  public
                    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  Doc, V, Bod: OleVariant;
  I1, I, X: integer;
  ms: TMemoryStream;
begin

  webBrowser_V11.navigate(Edit1.Text, V, V, V, V);
  //webBrowser_V11.navigate('about:blank',V,V,V,V);
  while webBrowser_V11.busy do
    application.processMessages;
  Doc := webBrowser_V11.Document;
  Bod := Doc.Body;
  Memo1.Text := Bod.InnerText;
  //Bod.InnerHTML := Memo1.Text;

                  //IE.Quit;
  with TFileStream.Create(ExtractFilePath(Application.exeName) + 'TextSavedSeenInView' + 'View'
    + '.txt', fmCreate) do

  try
    I := 0;
    X := Memo1.Lines.Count;
    while I < X do
    begin
      Memo1.Lines[I];
      I1 := Length(Memo1.Lines[I]);
      Write(Pointer(Memo1.Lines[I])^, I1);
      Inc(I);
    end;
  finally free end;
end;


procedure TForm1.FormShow(Sender: TObject);
begin
  Edit1.Text := 'https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20161215&jsessionid=200156996586977326';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.

Thanks again.

Delphi3