Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a secure certificate via the web browser control 1

Status
Not open for further replies.

Revolution1200

Programmer
Sep 29, 2005
143
GB
Hi All,

I have a small project that logs into a secure website via an embedded web control on my vb form.

We use this to automatically download call stats.

This works well except for one small problem, the site uses a secure certificate installed on my machine, so every time the program goes to update the stats i need to select the correct certificate to use.

Is there any way programatically to select the correct certificate automatically?

Kind Regards

Stuart
 
Ok, question 1, can you confirm which control you are using.

I'd suggest that you probably actually need to be using the Microsoft winHTTP Services library (which should be available under references). This includes a SetClientCertificate method.

 
Hi Strongm,

Sorry for the late reply - dont worek the weekends :).

I am currently using the WebBrowser control in VB6, the reason I used this was because it gave me access to the underlying DOM structure of the web pages allowing me to pull the call stats from the various tables on the web page.

Would the winHTTP allow me to do the same sort of thing?

Kind Regards

Stuart
 
Sure. You can simply load the results of the winHTTP request into a browser control, so there should be hardly any changes to your code ...
 
In fact have an example. Something like the following should work. You'll need a form with a command button, and to set references to Microsoft WinHHTP Services, Microsoft HTML Object library(and also a reference to Microsoft Internet Controls if you haven't added the the Microsoft Internet Controls to the control toolbox;note that for the example you don't actually need a web browser control on the form):
Code:
[blue]Option Explicit

'Dim WithEvents myRequest As WinHttpRequest ' if we wanted to handle events

Private Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0&
Private Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1&

Private Const HTTPREQUEST_PROXYSETTING_DEFAULT = 0
Private Const HTTPREQUEST_PROXYSETTING_PRECONFIG = 0
Private Const HTTPREQUEST_PROXYSETTING_DIRECT = 1
Private Const HTTPREQUEST_PROXYSETTING_PROXY = 2

Private Sub Command4_Click()
    Dim DemoInternetExplorer As InternetExplorer
    
    ' if you happen to have a reference to the Microsoft XML library
    ' you can use the following two lines instead of the winHTTP lines
    'Dim myRequest As ServerXMLHTTP50
    'Set myRequest = New ServerXMLHTTP50
    
    Dim myRequest As WinHttpRequest
    Set myRequest = New WinHttpRequest
    
    myRequest.SetClientCertificate "certificate_name"
    myRequest.open "GET", "[URL unfurl="true"]https://piper/secure/default.htm"[/URL]
    'myRequest.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "servername:port" ' if you have a proxie, since winHTTP doesn't use Internet settings ...
    myRequest.SetCredentials "username", "password", HTTPREQUEST_SETCREDENTIALS_FOR_SERVER ' if you are required to login as well
    myRequest.Send ' OK, let's do it
    
    ' We're doing it synchronously, so we need to wait
    Do Until myRequest.Status = 200
        DoEvents
    Loop
    
    Set DemoInternetExplorer = New InternetExplorer
    DemoInternetExplorer.navigate "about:blank"
    DemoInternetExplorer.document.write myRequest.ResponseText
    MsgBox DemoInternetExplorer.document.body.outerHTML
    ' at this point our IE object contains the right document
End Sub[/blue]
 
Hi Strongm,

Many thanks for the example, both the code above and the code i had put together give me the same error - "The host name on the certificate is invalid or does not match"

The site i am trying to access is "
I have tried -

myRequest.open "GET", "
And also

myRequest.open "GET", " Call Care System.htm"

but no joy with either. do you have any idea?

Thanks

Stuart
 
Thanks - oh well back to the drawing board!

Thanks for your help - have a star.

Stuart
 
Hi Strongm,

Got this working by adding the following 2 lines

Private Const IgnoreNameMismatch = 4096

myRequest.Option(SslErrorIgnoreFlagsOption) = IgnoreNameMismatch

Cheers

Stuart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top