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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTPS Post with wininet.dll

Status
Not open for further replies.

btaber

Programmer
May 26, 2002
307
US
I currently have some code that can post information to a web server, and retrieve the response. I am trying to make it do this using https, but it seams to die on httpSsendRequest. The code for the module is below. ANY help would be appreciated.

Brian





Option Explicit
DefLng A-Z

Const INTERNET_INVALID_PORT_NUMBER = 0
Const INTERNET_OPEN_TYPE_PRECONFIG = 0




' Number of the TCP/IP port on the server to connect to.
Private Const INTERNET_DEFAULT_FTP_PORT = 21
Private Const INTERNET_DEFAULT_GOPHER_PORT = 70
Private Const INTERNET_DEFAULT_HTTP_PORT = 80
Private Const INTERNET_DEFAULT_HTTPS_PORT = 443
Private Const INTERNET_DEFAULT_SOCKS_PORT = 1080

' Type of service to access.
Private Const INTERNET_SERVICE_FTP = 1
Private Const INTERNET_SERVICE_GOPHER = 2
Private Const INTERNET_SERVICE_HTTP = 3

' Brings the data across the wire even if it locally cached.
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_SECURE = &H800000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000


Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

Private Declare Function InternetReadFile Lib "wininet.dll" _
(ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _
lNumberOfBytesRead As Long) As Integer

Declare Function HttpSendRequest Lib _
"wininet.dll" Alias "HttpSendRequestA" _
(ByVal hHttpRequest As Long, ByVal sHeaders _
As String, ByVal lHeadersLength As Long, _
ByVal sOptional As String, ByVal _
lOptionalLength As Long) As Integer

Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long

Private Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" _
(ByVal hHttpSession As Long, ByVal sVerb As String, ByVal sObjectName As String, ByVal sVersion As String, _
ByVal sReferer As String, ByVal something As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long

Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer


Dim m_cPostBuffer As String


Public Function PostForm(ByVal server As String, ByVal CGI As String) As String

On Error GoTo myError

Dim hOpen As Long, hConnection As Long
Dim hURL As Long
Dim sBuffer As String
Dim lNumBytesToRead As Long
Dim lNumberOfBytesRead As Long
Dim Result As String

' open internet connection
hOpen = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0)
If hOpen <> 0 Then
'hConnection = InternetConnect(hOpen, server, INTERNET_INVALID_PORT_NUMBER, &quot;&quot;, &quot;&quot;, INTERNET_SERVICE_HTTP, 0, 0)
hConnection = InternetConnect(hOpen, server, INTERNET_DEFAULT_HTTPS_PORT, &quot;&quot;, &quot;&quot;, INTERNET_SERVICE_HTTP, 0, 0)
If hConnection <> 0 Then
'hURL = HttpOpenRequest(hConnection, &quot;POST&quot;, CGI, &quot;&quot;, &quot;&quot;, 0, (INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_SECURE), 0)
hURL = HttpOpenRequest(hConnection, &quot;POST&quot;, CGI, &quot;&quot;, &quot;&quot;, 0, INTERNET_FLAG_SECURE, 0)

MsgBox hURL
If hURL <> 0 Then
If HttpSendRequest(hURL, &quot;&quot;, 0, m_cPostBuffer, Len(m_cPostBuffer)) Then
lNumBytesToRead = 1024
sBuffer = Space$(lNumBytesToRead)
Do While InternetReadFile(hURL, sBuffer, lNumBytesToRead, lNumberOfBytesRead)
If lNumberOfBytesRead = 0 Then
Exit Do
Else
Result = Result & Left$(sBuffer, lNumberOfBytesRead)
End If
lNumBytesToRead = 1024
sBuffer = Space$(lNumBytesToRead)
Loop
PostForm = Trim$(Result)
Else
Err.Raise vbObjectError + 504, , &quot;HttpSendRequest&quot;
End If
Else
Err.Raise vbObjectError + 505, , &quot;HttpOpenRequest&quot;
End If
Else
Err.Raise vbObjectError + 506, , &quot;InternetConnect&quot;
End If
Else
Err.Raise vbObjectError + 507, , &quot;InternetOpen&quot;
End If

myExit:
InternetCloseHandle hURL
InternetCloseHandle hConnection
InternetCloseHandle hOpen
Exit Function

myError:
PostForm = &quot;ERROR &quot; & Err.Description
Resume myExit

End Function

Private Function UrlEncode(sText As String) As String

Dim sResult As String
Dim sFinal As String
Dim sChar As String
Dim i As Long

For i = 1 To Len(sText)

sChar = Mid$(sText, i, 1)

If InStr(1, &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;, sChar) <> 0 Then
sResult = sResult & sChar
ElseIf sChar = &quot; &quot; Then
sResult = sResult & &quot;+&quot;
ElseIf True Then
sResult = sResult & &quot;%&quot; & Right$(&quot;0&quot; & Hex(Asc(sChar)), 2)
End If

If Len(sResult) > 1000 Then
sFinal = sFinal & sResult
sResult = &quot;&quot;
End If

Next

UrlEncode = sFinal & sResult

End Function


Public Function AddPostKey(tckey As String, tcValue As String)
m_cPostBuffer = m_cPostBuffer & UrlEncode(tckey) & _
&quot;=&quot; + UrlEncode(tcValue) + &quot;&&quot;
End Function


 
I don't see anything in the httpsendrequest documentation that would make me think it would work for https.

I don't think the public/private encryption keys (or certificates I guess they are called) are built into the OS.

The WebBrowser control will navigate https sites but I think that's because it's basically just an IE automation and the encrytption keys are built into that.








 
Come to find out, actually is working for https. The wininet.dll is the same backend inferface that IE uses, so it contains everything for ssl already. There is a lot of flags that can be reas to tell if the certificate host name matches, the site gets redirected from https to http, etc.

If you specify https:// (or in front of the site, it would be unable to resolve the server. To make it communicate https, in InternetConnect, you have to change INTERNET_INVALID_PORT_NUMBER to INTERNET_DEFAULT_HTTPS_PORT and in HttpOpenRequest, INTERNET_FLAG_KEEP_CONNECTION to INTERNET_FLAG_SECURE. I had found this code in an article, but a lot of things were missing.

The best thing about this is that it does not require a ActiveX to function, and any computer with Internet Explorer has the wininet.dll. If anyone would like more info, check out MSDN:



Hope this helps!
Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top