I have the following WinHttp request code written in Excel VBA that opens an ASP.net page (using .NET CLR Version v4.0.30319) on my website which returns data in the JSON format.
The problem I'm experiencing is when WinHttp opens the URL, the Session and Application variables in the ASP.net webpage completely don't work. This causes the WinHttp request to hit errors in the webpage.
If I open the exact same URL using a web browser such as IE or Chrome, the page loads perfectly and returns the correct JSON data.
It appears the WinHttp request coming from Excel VBA is not recognized by IIS as being a valid browser worthy of access to the Session/Application objects.
How to solve this?
---- begin code sample ----
Dim strRequestType As String
Dim objHTTP As Object
If strPostParameters <> vbNullString Then
strRequestType = "POST"
Else
strRequestType = "GET"
End If
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
With objHTTP
.Open strRequestType, strURL, False
.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.SetRequestHeader "Content-type", "application/x-www-form-urlencoded"
If strPostParameters <> vbNullString Then
.Send (strPostParameters)
Else
.Send
End If
A session is identified with an id that is sent by the server and then sent back by the browser with each request. You would need to send the session identifier cookie as part of your web request.
It could be simpler to avoid to depend on session for serving this kind of content. What do you keep in session?
None
0 Points
2 Posts
WinHttp.WinHttpRequest.5.1 And The Mysterious Case Of The Disappearing Session Variables
May 06, 2018 12:08 AM|MatthewNYC|LINK
Hi,
I have the following WinHttp request code written in Excel VBA that opens an ASP.net page (using .NET CLR Version v4.0.30319) on my website which returns data in the JSON format.
The problem I'm experiencing is when WinHttp opens the URL, the Session and Application variables in the ASP.net webpage completely don't work. This causes the WinHttp request to hit errors in the webpage.
If I open the exact same URL using a web browser such as IE or Chrome, the page loads perfectly and returns the correct JSON data.
It appears the WinHttp request coming from Excel VBA is not recognized by IIS as being a valid browser worthy of access to the Session/Application objects.
How to solve this?
---- begin code sample ----
Dim strRequestType As String
Dim objHTTP As Object
If strPostParameters <> vbNullString Then
strRequestType = "POST"
Else
strRequestType = "GET"
End If
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
With objHTTP
.Open strRequestType, strURL, False
.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.SetRequestHeader "Content-type", "application/x-www-form-urlencoded"
If strPostParameters <> vbNullString Then
.Send (strPostParameters)
Else
.Send
End If
m_strResponseText = .ResponseText
m_strResponseStatus = .Status
---- end code sample ----
All-Star
48510 Points
18072 Posts
Re: WinHttp.WinHttpRequest.5.1 And The Mysterious Case Of The Disappearing Session Variables
May 06, 2018 09:52 AM|PatriceSc|LINK
Hi,
A session is identified with an id that is sent by the server and then sent back by the browser with each request. You would need to send the session identifier cookie as part of your web request.
It could be simpler to avoid to depend on session for serving this kind of content. What do you keep in session?