Article View: borland.public.delphi.com.activex.using
Article #8Re: TWebBrowser and SSL Certificate
From: michael.martinek
Date: Wed, 20 Jun 2007 06:40
Date: Wed, 20 Jun 2007 06:40
154 lines
6268 bytes
6268 bytes
As a work-around, I'm using the Indy IdHTTP component along with an
SSL IOHandler. For this to work properly, I have to run BDS and my
application with Windows XP emulation; as the SSL files required to be
used do not work under 64 bit; 32bit compatibility must be used for it
to work properly.
After downloading the page data via IdHTTP, it can be streamed into
IE. My testing code is as follows:
Uses IdHTTP, IdSSLOpenSSL, IdException;
function SIF(const ACondition: Boolean; const AIfTrue, AIfFalse:
String): String;
begin
if (ACondition) then
Result := AIfTrue
else
Result := AIfFalse;
end;
procedure Destroy_HTTPObject(var AObject: TIdHTTP);
begin
if (Assigned(AObject.IOHandler)) then
AObject.IOHandler.Free;
FreeAndNil(AObject);
end;
function Create_HTTPObject(): TIdHTTP;
var
SSLHandler: TIdSSLIOHandlerSocket;
begin
try
Result := TIdHTTP.Create(nil);
Result.HandleRedirects := TRUE;
SSLHandler := TIdSSLIOHandlerSocket.Create(nil);
SSLHandler.SSLOptions.Mode := sslmClient;
SSLHandler.SSLOptions.VerifyMode := [];
SSLHandler.SSLOptions.Method := sslvTLSv1;
Result.IOHandler := SSLHandler;
except
Destroy_HTTPObject(Result);
end;
end;
function URLDownloadToString(const AURL: String; var AOutputBuffer:
String): Boolean;
var
oHTTP: TIdHTTP;
sURL: String;
begin
AOutputBuffer := '';
Result := FALSE;
try
//make sure there is a protocol of some sort, if not default to
HTTP
sURL := SIF(Pos('://', AURL) = 0, 'http://' + AURL, AURL);
oHTTP := Create_HTTPObject();
if (Assigned(oHTTP)) then
begin
AOutputBuffer := oHTTP.Get(sURL);
Result := TRUE;
end;
finally
Destroy_HTTPObject(oHTTP);
end;
end;
To prevent IE from downloading any additional data from an HTTPS-
enabled site, an internet session handler can be implemented to
intercept outbound connection requests, and instead utilize the
downloading function to go out and retrieve the data, then return the
raw data for the IE component to use.
The whole ".Silent" property is a bloody joke. Results seem to vary,
without changing the flag state. Sometimes when attempting to visit a
site with invalid SSL certs, a dialog box comes up prompting for
approval of the cert. Other times, a new IE browser window pops up
with the same prompt information, but in an HTML format. The new
window loads, regardless of setting ppDisp in the NewWindow2 and
NewWindow3 events. To accept via the dialog, I was using:
procedure TForm2.EmbeddedWB1ShowDialog(Sender: TObject; h: Cardinal);
var
iLength: Integer;
sCaption: String;
sDlgName: String;
hButtonYes: HWND;
begin
iLength := GetWindowTextLength(H);
SetLength(sCaption, iLength + 1);
GetWindowText(h, PChar(sCaption), iLength + 1);
SetLength(sDlgName, 80);
SetLength(sDlgName, GetClassName(h, PChar(sDlgName),
Length(sDlgName)));
if ((sDlgName = '#32770') and (Pos('Security', sCaption) > 0)) then
begin
hButtonYes := FindWindowEx(h, HWND(0), 'BUTTON', '&Yes');
if (hButtonYes = HWND(0)) then
hButtonYes := FindWindowEx(h, HWND(0), 'BUTTON', 'Yes');
if (hButtonYes <> HWND(0)) then
begin
SendMessage(hButtonYes, WM_LBUTTONDOWN, 0, 0);
SendMessage(hButtonYes, WM_LBUTTONUP, 0, 0);
end;
end;
end;
Using the TEmbeddedWB component, I'd STRONGLY advise against using
the .Wait() method. It took me a long time to find out WHY it kept
popping up windows recursively. It turns out that calling the .Wait()
was doing it, as it would loop and check WebBrowser.ReadyState every
time. This apparently caused the web browser to attempt to reload the
document which had halted loaded due to the cert security issue. To
fix this, I changed the loop:
FWebBrowser.Navigate(urlEntry.URL);
//convert timeout and delays to configurable options at a later
time.
dwTimeoutSpent := 0;
dwTimeout := 10 * 1000;
dwTimeoutDelay := 100; //ms
repeat
oDocument2 := FWebBrowser.Document as IHTMLDocument2;
Sleep(dwTimeoutDelay);
Application.ProcessMessages();
Inc(dwTimeoutSpent, dwTimeoutDelay);
until ((dwTimeoutSpent > dwTimeout) or ((Assigned(oDocument2)
and (FWebBrowser.ReadyState = READYSTATE_COMPLETE))));
This enabled both a chance to check for a timeout, and to also abort
the loop if the document finished loading. This also does not cause
the component to attempt navigation again.
Questions, comments, or any input would be appreciated. I'd still like
to figure out a way to use the actual IE component and get that
working. If you use this code or variations of it, I'd appreciate
knowing beforehand. Simply because I'd like to know whether or not
anyone benefits from my days of researching and debugging a topic that
appears to have little to no information available on it.
Regards,
Michael Martinek
Technical Details
Message-ID:
<1182321625.702837.269810@q19g2000prn.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!q19g2000prn.googlegroups.com!not-for-mailReferences:
<1182201541.046947.180420@i13g2000prf.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 123df9,e59b00f65ce201c4 X-Google-Attributes: gid123df9,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!q19g2000prn.googlegroups.com!not-for-mail From: michael.martinek@gmail.com Newsgroups: borland.public.delphi.com.activex.using Subject: Re: TWebBrowser and SSL Certificate Date: Tue, 19 Jun 2007 23:40:25 -0700 Organization: http://groups.google.com Lines: 155 Message-ID: <1182321625.702837.269810@q19g2000prn.googlegroups.com> References: <1182201541.046947.180420@i13g2000prf.googlegroups.com> NNTP-Posting-Host: 24.18.220.88 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1182321626 16087 127.0.0.1 (20 Jun 2007 06:40:26 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 20 Jun 2007 06:40:26 +0000 (UTC) In-Reply-To: <1182201541.046947.180420@i13g2000prf.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: q19g2000prn.googlegroups.com; posting-host=24.18.220.88; posting-account=p8944g0AAACoi39-8ZdwcfcpOv-qAkfd