Solved

Timeout problems and general unreliability using msxml

Posted on 2006-06-22
6
387 Views
Last Modified: 2012-05-05
Hi all,

This is turning into a real annoyance. I am trying to use MSXML server objects to load ASP pages into my website template. This works about 80% of the time, really fast and efficient, but the other 20%, the script just times out. I'm connecting to a page within the same web server, so there shouldn't really be any network traffic issues. I've tried with MSXML2.ServerXMLHTTP v 2, 3, 4, and 6 and they all suffer the same unreliability.
I'm using these server side (obviously). I reckon the problem sort of lies with the While....Wend statement but don't really know how to fix it.

My Script is below, though because it is part of a bigger class, some of the variables etc may not be obvious. Need all the stuff to do with cookies and referers due to login post-thru requirements.

Hope you can help!!


Public Sub GetXMLHTTP(vDoc, vType)
      Dim oXMLHTTP, vReferer, vHTTP
      Set oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
      If Request.ServerVariables("HTTPS") = "on" Then vHTTP = "https://" : Else vHTTP = "http://"

      vReferer = vHTTP & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("SCRIPT_NAME") & "?" & Request.QueryString
      If len(pCookies) = 0 Then pCookies = "nothing=nothing"

      If CheckTextRequest(pPost) Then pFormPost = pFormPost & pPost
      If CheckTextRequest(CStr(Request.Form)) Then pFormPost = pFormPost & CStr(Request.Form)
            Err.Clear()
            oXMLHTTP.Open "POST", vDoc, False
            oXMLHTTP.SetRequestHeader("Cookie") = "nothing=nothing"
            oXMLHTTP.SetRequestHeader("Cookie") = pCookies
            If CheckTextRequest(pFormPost) Then
                  oXMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
                  oXMLHTTP.SetRequestHeader "Referer", vReferer
                  oXMLHTTP.Send CStr(pFormPost)
            Else
                  oXMLHTTP.Send
            End If      

            While oXMLHTTP.ReadyState <> 4
                  oXMLHTTP.WaitForResponse 1000
            Wend
      
      Dim strHeaders, hArr
      strHeaders = oXMLHTTP.getAllResponseHeaders()

    hArr = split(strHeaders,"Set-Cookie: ")
      For kk = 1 to ubound(hArr)  
          theCookie = left(hArr(kk),instr(hArr(kk),"path=/")-2)  
          pCookies = pCookies & " " & theCookie
      Next
      
      Select Case vType
            Case 1            Set oXMLDoc = oXMLHTTP.ResponseXML
            Case 2            oXMLDocText = replace(oXMLHTTP.responsetext,"../",baseURL)
            Case Else      Set oXMLDoc = oXMLHTTP.ResponseXML
                              oXMLDocText = replace(oXMLHTTP.responsetext,"../",baseURL)
      End Select
      Set oXMLHTTP = Nothing
End Sub

0
Comment
Question by:wildtangent
  • 2
  • 2
6 Comments
 
LVL 14

Expert Comment

by:CyrexCore2k
Comment Utility
Change This:

oXMLHTTP.Open "POST", vDoc, False

To

oXMLHTTP.Open "POST", vDoc, True
0
 

Author Comment

by:wildtangent
Comment Utility
Hi, I've tried both of those options but it doesn't seem to make a lot of difference. The errors occur pretty inconclusively randomly, though seem to be less frequent with msxml6 installed.

Cheers tho, any more suggestions?
0
 
LVL 14

Accepted Solution

by:
CyrexCore2k earned 500 total points
Comment Utility
Well this part of your code negates the effects:

While oXMLHTTP.ReadyState <> 4
               oXMLHTTP.WaitForResponse 1000
Wend

Basically you need a timeout function of your own.

Function dataRetrieved(xmlHTTP, Location, Cookies, Referer, Postdata)
          dataRetrieved = False
          XMLHTTP.Open "POST", Location, True
          XMLHTTP.SetRequestHeader("Cookie") = "nothing=nothing"
          XMLHTTP.SetRequestHeader("Cookie") = Cookies
          If CheckTextRequest(Postdata) Then
               XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
               XMLHTTP.SetRequestHeader "Referer", Referer
               XMLHTTP.Send CStr(pFormPost)
          Else
               XMLHTTP.Send
          End If    

          startTime = now()
          While XMLHTTP.ReadyState <> 4 and _
               DateDiff("s", startTime, now()) < 2
          Wend
          If XMLHTTP.ReadyState = 4 Then
               dataRetrieved = True
          End If
End Function

Public Sub GetXMLHTTP(vDoc, vType)
     Dim oXMLHTTP, vReferer, vHTTP, Retries
     Retries = 3

     Set oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

     If Request.ServerVariables("HTTPS") = "on" Then vHTTP = "https://" : Else vHTTP = "http://"

     vReferer = vHTTP & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("SCRIPT_NAME") & "?" & Request.QueryString
     If len(pCookies) = 0 Then pCookies = "nothing=nothing"

     If CheckTextRequest(pPost) Then pFormPost = pFormPost & pPost
     If CheckTextRequest(CStr(Request.Form)) Then pFormPost = pFormPost & CStr(Request.Form)
          Err.Clear()
          Do While Not dataRetrieved(oXMLHTTP, vDoc, pCookies, vReferer, pFormPost) and Retries > 0
               Retries = Retries - 1
          Loop
          If oXMLHTTP.ReadyState <> 4 Then
               Response.Write "Failed to open address."
               Response.End
          End If
     Dim strHeaders, hArr
     strHeaders = oXMLHTTP.getAllResponseHeaders()

    hArr = split(strHeaders,"Set-Cookie: ")
     For kk = 1 to ubound(hArr)  
         theCookie = left(hArr(kk),instr(hArr(kk),"path=/")-2)  
         pCookies = pCookies & " " & theCookie
     Next
     
     Select Case vType
          Case 1          Set oXMLDoc = oXMLHTTP.ResponseXML
          Case 2          oXMLDocText = replace(oXMLHTTP.responsetext,"../",baseURL)
          Case Else     Set oXMLDoc = oXMLHTTP.ResponseXML
                         oXMLDocText = replace(oXMLHTTP.responsetext,"../",baseURL)
     End Select
     Set oXMLHTTP = Nothing
End Sub


The timeout function gives the object two seconds to download the source. You may want to adjust this depending on the size of the pages you're downloading.
0
 

Author Comment

by:wildtangent
Comment Utility
looks good, will try tomorrow. Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now