Handling server responses with WinHttpRequest object
Yesterday I spent few hours trying to understand why my application was crashing after receive a big responses from server, in the end it was a question of how to use WinHttpRequest object.
WinHttpRequest is a COM object that can be used to send and receive data from server via HTTP protocol, I use it a lot in my search application because I need send user queries to search server and then process the responses in xml format.
Look this piece of code that I used to send HTTP requests to my search server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Dim objXMLHTTP Set objXMLHTTP = Server.CreateObject("WinHttp.WinHttpRequest.5.1") objXMLHTTP.Open "GET" , "/bigfile.xml" , false objXMLHTTP.setRequestHeader "Content-Type", "text/html; charset=utf-8" objXMLHTTP.Send Dim objXML Set objXML = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0") objXML.validateOnParse = false objXML.resolveExternals = false objXML.preserveWhiteSpace = false objXML.async = false objXML.LoadXML( objXMLHTTP.ResponseText ) |
The code above works fine in many situations but if your server response gets bigger over the time you may get this error:
1 2 3 | WinHttp.WinHttpRequest error '8007000e' Not enough storage is available to complete this operation. /QueryData.asp, line 273 |
That means your search response is big enougth to load it by using ResponseText property of WinHttpRequest (line 14), this property has a limitation in number of characters returned from server when you are handling HTTP requests in synchronous mode.
Here’s the modifed version of code, now I can handle big responses properly:
1 2 3 4 5 | '... 'Same as above '... objXML.Load( objXMLHTTP.ResponseStream ) |
Now I try to load the server response as stream (line 5), this approach scales very well because I’m not trying to load the server response in one step, instead of this, I’m trying to load it in several chunks and doesn’t matter how much bigger this response is.