Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Re: Create webpage from a string in Delphi


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Jul 13, 2017 2:59 PM Last Post By: Antonio Estevez
Cornelia von Sc...

Posts: 7
Registered: 11/4/16
Re: Create webpage from a string in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 12, 2017 12:42 PM
I am dynamically creating a TWebrowser control and displaying a webpage whose html code is defined in a local file on my machine ('C:\AdagioSource\d10\AR\source\GetCardScreen.html').
What I really would like to do is to define the webpage's html code as a widerstring and create it programatically. But the Navigate command in Delphi requires a URL as input or a path to a file on your machine, it does not work when the input
is a string. This is apparently possible in PHP where a page can be defined as a constant.
Any ideas?
Thanks
Cornelia


var GetCreditCardPage: TWebBrowser;
try
GetCreditCardPage := TWebBrowser.Create(frmParent);
TWinControl(GetCreditCardPage).Parent := frmParent;
GetCreditCardPage.Align := alNone;/

if assigned(GetCreditCardPage) then
begin
GetCreditCardPage.Navigate('C:\AdagioSource\d10\AR\source\GetCardScreen.html');
GetCreditCardPage.OnCommandStateChange := GetCreditCardPageCommandStateChange;
GetCreditCardPage.OnNavigateError := GetCreditCardPageNavigateError;
end
else
begin
Result := APS_VC_STEP_TWO_NO_FORM;
EXIT;
end;
Application.ProcessMessages;

// I do some other stuff here

finally
if bWebBrowserError or bRedirectComplete then
begin
if assigned(GetCreditCardPage) then
begin
GetCreditCardPage.FreeOnRelease;
GetCreditCardPage.Free;
end;
end;
end;

Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Create webpage from a string in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 12, 2017 1:46 PM   in response to: Cornelia von Sc... in response to: Cornelia von Sc...
El 12/07/2017 a las 21:42, Cornelia von Schellwitz escribió:
I am dynamically creating a TWebrowser control and displaying a webpage whose html code is defined in a local file on my machine ('C:\AdagioSource\d10\AR\source\GetCardScreen.html').
What I really would like to do is to define the webpage's html code as a widerstring and create it programatically. But the Navigate command in Delphi requires a URL as input or a path to a file on your machine, it does not work when the input
is a string. This is apparently possible in PHP where a page can be defined as a constant.
Any ideas?

if this is VCL project then you can load the html code int the web browser from a stream and you can create the stream
from a string using the TStringStream class:

uses
...
  ActiveX,
...
 
var
   PSI: IPersistStreamInit;
   Adapter: TStreamAdapter;
   Stream: TStream;
   Html: String;
begin
...
   GetCreditCardPage.Navigate('about:blank');
   while (GetCreditCardPage.ReadyState< Shdocvw.READYSTATE_LOADED) do
     Application.ProcessMessages;
 
   Html:= '<html><body>This is a test</body></html>';
 
   if Succeeded(GetCreditCardPage.Document.QueryInterface(IPersistStreamInit, PSI)) then
   begin
     Stream:= TStringStream.Create(Html);
     Adapter:= TStreamAdapter.Create(Stream, soOwned);
     PSI.Load(Adapter);
   end;
...
end;


Edited by: Antonio Estevez on Jul 12, 2017 1:51 PM
Cornelia von Sc...

Posts: 7
Registered: 11/4/16
Re: Create webpage from a string in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2017 11:09 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio Estevez wrote:
El 12/07/2017 a las 21:42, Cornelia von Schellwitz escribió:
I am dynamically creating a TWebrowser control and displaying a webpage whose html code is defined in a local file on my machine ('C:\AdagioSource\d10\AR\source\GetCardScreen.html').
What I really would like to do is to define the webpage's html code as a widerstring and create it programatically. But the Navigate command in Delphi requires a URL as input or a path to a file on your machine, it does not work when the input
is a string. This is apparently possible in PHP where a page can be defined as a constant.
Any ideas?

if this is VCL project then you can load the html code int the web browser from a stream and you can create the stream
from a string using the TStringStream class:

uses
...
  ActiveX,
...
 
var
   PSI: IPersistStreamInit;
   Adapter: TStreamAdapter;
   Stream: TStream;
   Html: String;
begin
...
   GetCreditCardPage.Navigate('about:blank');
   while (GetCreditCardPage.ReadyState< Shdocvw.READYSTATE_LOADED) do
     Application.ProcessMessages;
 
   Html:= '<html><body>This is a test</body></html>';
 
   if Succeeded(GetCreditCardPage.Document.QueryInterface(IPersistStreamInit, PSI)) then
   begin
     Stream:= TStringStream.Create(Html);
     Adapter:= TStreamAdapter.Create(Stream, soOwned);
     PSI.Load(Adapter);
   end;
...
end;


Edited by: Antonio Estevez on Jul 12, 2017 1:51 PM

Thanks for your help. I am trying your suggestion but have some difficulty with TStringStream, which I have never used.
I want to create a TStringStream which is the concatenation of the 4 constants defined below.
How would you do this?
Gracias por tu ayuda, de donde eres?
Cornelia

CreditCardScreenDocType = '<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml">';
CreditCardScreenHead = '<head><meta charset="utf-8" /><title></title><style></style></head>';
CreditCardScreenBody1='<body>

<form name="ccForm action=" + url2 + " method="post"><label for="cc">Credit Card Number:</label>';
CreditCardScreenBody2='
<input type="text" id="cc_number" pattern="
d*" title="Numbers Only" value="" name="cc_number" maxlength="16" required>';
CreditCardScreenBody3='
<label for="xmonth">Expiration Date:</label>
<input type="month" id="cc_exp" name="cc_exp" required>

<input type="submit" id="submit" value="Submit"></form>
</body></html>';
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Create webpage from a string in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2017 11:36 AM   in response to: Cornelia von Sc... in response to: Cornelia von Sc...
El 13/07/2017 a las 20:09, Cornelia von Schellwitz escribió:

Thanks for your help. I am trying your suggestion but have some difficulty with TStringStream, which I have never used.
I want to create a TStringStream which is the concatenation of the 4 constants defined below.
How would you do this?
Gracias por tu ayuda, de donde eres?
Cornelia

CreditCardScreenDocType = '<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml">';
CreditCardScreenHead = '<head><meta charset="utf-8" /><title></title><style></style></head>';
CreditCardScreenBody1='<body>

<form name="ccForm action=" + url2 + " method="post"><label for="cc">Credit Card Number:</label>';
CreditCardScreenBody2='
<input type="text" id="cc_number" pattern="
d*" title="Numbers Only" value="" name="cc_number" maxlength="16" required>';
CreditCardScreenBody3='
<label for="xmonth">Expiration Date:</label>
<input type="month" id="cc_exp" name="cc_exp" required>

<input type="submit" id="submit" value="Submit"></form>
</body></html>';

You can assign the concatenation of the 5 constants to the Html variable and then pass the Html variable to the
TStringStream's constructor:

   Html:= CreditCardScreenDocType +
          CreditCardScreenHead +
          CreditCardScreenBody1 +
          CreditCardScreenBody2 +
          CreditCardScreenBody3;
 
   if Succeeded(GetCreditCardPage.Document.QueryInterface(IPersistStreamInit, PSI)) then
   begin
     Stream:= TStringStream.Create( Html );  // <---
     Adapter:= TStreamAdapter.Create(Stream, soOwned);
     PSI.Load(Adapter);
   end;


or you can concatenate the constants in the TStringStream's constructor directly:

   if Succeeded(GetCreditCardPage.Document.QueryInterface(IPersistStreamInit, PSI)) then
   begin
     Stream:= TStringStream.Create( CreditCardScreenDocType +
                                    CreditCardScreenHead +
                                    CreditCardScreenBody1 +
                                    CreditCardScreenBody2 +
                                    CreditCardScreenBody3 );
     Adapter:= TStreamAdapter.Create(Stream, soOwned);
     PSI.Load(Adapter);
   end;


On the other hand, the definition of the CreditCardScreenBody1 constant is incorrect:
- You are missing the closing double quotes for the form's name attribute
- You are missing two single quotes: one before the first plus sign and other after the second plus sign.

  CreditCardScreenBody1='<body><div id="div1"><form name="ccForm" action="' + url2 + '" method="post"><label for="cc">Credit Card Number:</label>';


Note that this line will only be valid if url2 is another constant defined previously.
if url2 is a variable then you must split CreditCardScreenBody1 into 2 constants. By example:
  CreditCardScreenBody1A='<body><div id="div1"><form name="ccForm" action="';
  CreditCardScreenBody1B='" method="post"><label for="cc">Credit Card Number:</label>';


and then:
  Html:= CreditCardScreenDocType +
         CreditCardScreenHead +
         CreditCardScreenBody1A + url2 + CreditCardScreenBody1B +
         CreditCardScreenBody2 +
         CreditCardScreenBody3;


Edited by: Antonio Estevez on Jul 13, 2017 12:26 PM
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Create webpage from a string in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2017 12:37 PM   in response to: Cornelia von Sc... in response to: Cornelia von Sc...
El 13/07/2017 a las 20:09, Cornelia von Schellwitz escribió:
Gracias por tu ayuda, de donde eres?
Cornelia

Spain / España
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Create webpage from a string in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2017 2:59 PM   in response to: Cornelia von Sc... in response to: Cornelia von Sc...
I just readed your thread "Detect submit button press on a web form in delphi" and I understand what you are trying to do.

I have written an answer in it where I explain how to post the form data to the server and get the response without
using a webbrowser.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02