Databases - Delphi Web Clients

It is possible to "write your own" web browser in Delphi with almost no code - just use the Microsoft Internet Explorer ActiveX engine - aka TWebBrowser impemented via Microsoft’s Shell Doc Object and Control Library (SHDOCVW.DLL).

Well, this may seem a little like a cheat (it is!), but I had a requirement to interface a barcode reader (via a ComPort) to a database via the network. Developing an html/idc/htx interface to the data was simple enough, but there was no simple way to connect the barcode reader using either Internet Explorer or Netscape.

This page presents several ways to accomplish this


TWebBrowser

The TWebBrowser component (located on the Internet tab) will display a web page. Displaying the contents of a web page is trivial. Notice that I've used variables for all the WebBrowser1.Navigate parameters - this is required because Navigate will not accept constants. There are numerous problems with this Notes:


View Source

This shows how to display the contents of the retrieved page. I don't know where DOM.Body.OuterHTML is documented ... but it works ... sort of.

The problem is that the source is reformatted!

becomes References:


Posting Data

Posting data is significantly more difficult than Getting it ... mostly because the Delphi help provides no information on how to do it.

When a form Gets data, the query parameters are added to the html address (the question mark - ? - marks the beginning of the parameters and the ampersand - & - separates the individual parameters.)

When the data is Posted, the parameters are NOT displayed in the url.

So, other than hiding the parameters, why do we care? Well, it appears that ColdFusion is capable of accepting only Posted parameters and ignoring those attached to the url.

Unfortunately, if you follow the instructions in the Delphi 5 help, the Post fails. A solution is presented in Delphi tips and FAQs. Basically,

I have verified that this works. Of course, none of this is documented in the Delphi help.

Note: When TargetFrameName is a null string, and the target page has a security certificate that you must manually accept, the parameters do not work the first time that the page is called - they work fine after that (ie the second time Navigate is called).


References


TClientSocket

TClientSocket allows your application to function as a TCP/IP client. When the port is set to 80, this implements a direct interface to a web server, however, you have to render the results (which, of course, is the point).

Assuming that you are expecting data (as opposed to a formatted web page), you will want to include some kind of delimiters to simplify parsing the file. xml is one standard that can be used for that purpose.

Use a TWinSocketStream object to read from the connection when ClientType=ctBlocking.

TCustomWinSocket.ReceiveText works when ClientType=ctNonBlocking.

ReceiveText is not listed as a method of TClientSocket which is derived from TCustomSocket. There are 2 main types of sockets

TObject -> TPersistent -> TComponent -> 
           TAbstractSocket -> TCustomSocket -> TClientSocket
TObject -> TCustomWinSocket ->TClientWinSocket

See the CHAT demo for an example of how to use TClientSocket.


TNMHTTP

Located on the FastNet tab (Delphi 5 Pro), TNMHTTP allows you to read the text of a web page without displaying it.

This code will create a file in the same directory as the exe-file.

Note: Since FastNet is not bundled with Delphi 7, TNMHTTP is also not available (unless you BUY it - $200).


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Databases / Delphi / WebClients.html