PDA

View Full Version : WinHttpRequest with HTTPS



rintimtim
04-19-2009, 12:28 PM
Hi there,

I'm trying to make a HttpRequest to an HTTPS-Site but I always get an error. Connecting to a HTTP-Site works fine so it has to be a S(ecurity) issue. I've also set some options for the connection but it didn't help...Here's the code..i hope you can help me


Set httpObject = CreateObject("WinHttp.WinHttpRequest.5.1")
httpObject.Option(4) = 256 + 512 + 4096 + 8192
httpObject.Open "GET", "ht" & "tps" & ":" & "//" & "ww" & "w." & "xin" & "g.co" & "m", "false"
httpObject.send
MsgBox httpObject.GetAllResponseHeaders


P.S: Sorry for the tokenized url, but i was not allowed to post the real url here (since my post count is to low) so i had to split it up

stanl
04-20-2009, 07:56 AM
if it is an HTTPS site, look at the setcredentials property.

Kenneth Hobs
04-20-2009, 09:37 AM
Since you are using late binding, you may not know how to use the command that stanl explained.

Here is an early binding example. After you set the reference in Tools > References, you can type Request. to set that.
'early binding
Sub D1to2(z1, z2, ByRef dist As Double, ByRef tHour As Integer, ByRef tMin As Integer)
'requires reference to winhttp.dll in Microsoft WinHTTP Services, version 5.1
Dim Request As New WinHttpRequest, s As String
Dim sa() As String

'CountryCode=1 is United Kingdom
Request.Open "GET", _
"http://www.multimap.com/directions/?qs_1=" & CStr(z1) & _
"&countryCode_1=GB&qs_2=" & CStr(z2) & _
"&countryCode_2=GB&mode=driving&optimizeFor=time", _
False
Request.Send
Request.WaitForResponse

'Get body of text and parse to byref variables
s = Request.ResponseText
dist = MidStr(s, ":", "," & """miles") 'Distance in km
sa = Split(Mid(MidStr(s, "duration""" & ":{", "},""bounds"), 10), ":")
tHour = GetNumber(sa(1))
tMin = GetNumber(sa(2))
'tday = GetNumber(sa(3)) 'How to get days
End Sub

'Finds mid string from sTo and then back to sFrom. So, make sTo unique.
Function MidStr(str As String, sFrom As String, sTo As String, Optional toOffset As Integer = 0) As String
Dim strSub As String, sBegPos As Long, sEndPos As Long

sEndPos = InStr(str, sTo) - toOffset
strSub = Left(str, sEndPos)
sBegPos = InStrRev(strSub, sFrom) + 1

MidStr = Mid(strSub, sBegPos, sEndPos - sBegPos)
End Function


'Derk, ozgrid.com, 65763
Function GetNumber(s As String)
Dim j As Long
While Not IsNumeric(Left(s, 1))
If Len(s) <= 1 Then
Exit Function
Else
s = Mid(s, 2)
End If
Wend
GetNumber = Val(s)
End Function

stanl
04-20-2009, 12:40 PM
What I meants was, some HTTP(S)... ecure sites require authentication - see

http://msdn.microsoft.com/en-us/library/aa384058(VS.85).aspx