Where to put the sleep function in MSXML request? - vbscript

I use the following function to check if a RSS url is healthy then consume it:
function testUrl(url)
testUrl=0
Set o = CreateObject("MSXML2.XMLHTTP")
on error resume next
o.open "GET", url, false
o.send
if o.Status = 200 then testUrl = 1
on error goto 0
set o=nothing
end function
However when the target URL does not respond in a short time I will get timeout error. So I want to use the following function in this Q/A to terminate the request after 5 seconds if there was no success response but I dont know where to put the asp_Wait(5) and how to cancel the request after 5 seconds? Should I put asp_Wait right after the o.send or o.send acts synchronous?
Function asp_Wait(nMilliseconds)
Dim oShell
Set oShell= Server.CreateObject("WScript.Shell")
Call oShell.run("ping 1.1.1.1 -n 1 -w " & nMilliseconds,1,TRUE)
End Function

If using the WinHTTPRequest object you can call the WaitForResponse method.
Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", url, true 'async request
o.send
If o.waitForResponse(5) Then 'wait 5 sec
...
Else 'wait timeout exceeded
...
End If

Related

how to use MSXML2 setTimeouts to prevent timeout error?

I use the following function to check if a URL responds in a few seconds:
function testUrl(url)
Set xmlDOM = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlDOM.Open "GET", url, False
xmlDOM.setTimeouts 1000,1000,1000,1000
testUrl=xmlDOM.Send
end function
if testUrl("http://khabarfoori.com/rss/mm") then
responsw.write "active"
else
response.write "inactive"
end if
Instead of getting "active" or "inactive" I get the following error:
> msxml6.dll error '80072ee2'
> The operation timed out
Footnote: The tested URL above buffers a big amount of text with no server error. Is it a special case and I need more code to handle this kind of response?
May be this can did the trick !
feed = "http://khabarfoori.com/rss/mm"
Set req = CreateObject("Msxml2.ServerXMLHTTP.6.0")
req.Open "GET", feed, False
req.Send
Set xml = CreateObject("Msxml2.DOMDocument")
xml.loadXml(req.responseText)
First_Title = xml.getElementsByTagName("channel/item/title")(0).Text
If Len(First_Title) <> 0 Then
MsgBox "active"
MsgBox First_Title
else
MsgBox"inactive"
End If

Sending GET requests in VBScript loop fails

I'm setting up a client code written VBScript which probes a text file on a server with HTTP GET requests. The client has actions related to each HTTP response text.
While doing so in a While True loop, the first request returns the correct value. All the preceding requests return the same value the first response did.
The content of the file changes while the HTTP request doesn't seem to leave the client while sniffing packets.
Code:
function checkLog(url)
Dim WshShell, http
Set WshShell = CreateObject("WScript.Shell")
Set http = CreateObject("Microsoft.XmlHttp")
On Error Resume Next
http.open "GET" , url, False
http.send ""
checkLog = http.responseText
End function
Dim lastVal = "2"
Dim logResult = "2"
Do While True
logResult = checkLog("http://10.0.0.3/log.txt")
If logResult <> lastVal Then
If logResult = "0" Then
' Go Func 1
MsgBox "Got 0"
lastVal = logResult
End If
If logResult = "1" Then
' Go Func 2
MsgBox "Got 1"
lastVal = logResult
End If
End If
Sleep 5
LOOP
I expect that a packet will be sent every 5 seconds.
Thanks!
Disable caching of the HTTP response.
Set req = CreateObject("Msxml2.XmlHttp")
On Error Resume Next
req.Open "GET", url, False
req.SetRequestHeader "Cache-Control", "no-cache"
req.Send ""
checkLog = req.ResponseText

Timeout for a HTTP Resquest

Is there another ProgID instead of "MSXML2.XMLHTTP" that lets you set a timeout in VBScript?
OnReadyStateChange or similar must also work in VBScript.
Set oHTTP = CreateObject("MSXML2.XMLHTTP")
oHTTP.timeout = 10000 'Throws an error
oHTTP.Open "GET", "http://www.google.com", True
oHTTP.OnReadyStateChange = GetRef("oHTTP_OnReadyStateChange")
Sub oHTTP_OnReadyStateChange
' do something
End sub
oHTTP.Send
You can use either ServerXmlHttp with setTimeouts, or just use windows.XMLHttpRequest instead.

VB6 - WinHttpRequest Timouts Problem

I'm using a WebRequest in VB6 and I have the timeouts set at "5000" (5 seconds), but even after 5 seconds it is not timing out, any help is appreciated.
x:
Dim objWinHTTP
Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHTTP.Open "POST", "http://twitter.com/" & TwitterUSERNAME
objWinHTTP.SetTimeouts 5000, 5000, 5000, 5000
Call objWinHTTP.Send(psData)
MsgBox (objWinHTTP.Status)
If objWinHTTP.Status <> 200 Then
Timer.Enabled = False
MsgBox ("D:")
GoTo x
End If
you should set objWinHTTP.SetTimeouts before objWinHTTP.Open
and the four parameters you set for objWinHTTP.SetTimeouts are
Resolve, Connect, Send and Receive
So each value should be set according to that.

VBS Microsoft.XMLHTTP status

Why does the following code give a 80004005 error when run? I'm trying to get the status of several sites every 10 seconds...(the ones given are examples).
'http://www.sebsworld.net/information/?page=VBScript-URL
'http://www.paulsadowski.com/wsh/xmlhttp.htm
'the array of sites
sites = Array("http://www.google.com/","http://en.wikipedia.org/wiki/Main_Page")
While(True)
For Each site In sites
'Get site status
Set Http = WScript.CreateObject("Microsoft.XMLHTTP")
Http.Open "GET", site, True
Http.Send
If(Http.Status <> 200) Then 'site isn't 200
MsgBox "The site at " & vbNewLine & site & vbNewLine & "has status: " & Http.Status
End If
Next
WScript.Sleep(10)'Sleep 10 seconds
Wend
First, you have to change
Http.Open "GET", site, True
to
Http.Open "GET", site, False
because you cannot use Http.Status immediately after Http.Send if the call is asynchronous.
Furthermore, you shoud use
Set Http = WScript.CreateObject("MSXML2.ServerXMLHTTP")
instead of
Set Http = WScript.CreateObject("Microsoft.XMLHTTP")
because the normal XMLHTTP object has problems with redirected web sites (www.google.com normally redirects you to another site).
Const ForWriting = 2
strURL="http://asithayomal.1apps.com"
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", strURL, FALSE)
objHTTP.Send
msgbox objHTTP.ResponseText

Resources