I'm creating an QuickTest script that get all URL's for a page and check if the status of http request where code is 200. But my question is how do I get the http request status code from a URL ?
This was my solution:
Set Http = CreateObject("Microsoft.XMLHttp")
Set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
Set EditCollection = Browser("Browser").Page("Page").ChildObjects(oDesc)
URL_TEMP = EditCollection(i).GetROProperty("href")
Http.Open "GET" , URL_TEMP , false
Http.Send
msgbox Http.Status
Related
In Lotus a lotusscript agent is using this piece of code from a ScriptLibrary:
Set objDom = CreateObject("MSXML2.DOMDocument")
Set objXmlHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
Call objXmlHttp.SetTimeouts("240000", "240000", "240000", "240000")
objXmlHttp.Option(9) = 2048
objXmlHttp.Option(6) = True
objDom.async = False
objDom.LoadXml XmlBody
objXmlHttp.open "POST", Url, False
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "Content-Length", Len(XmlBody)
Status = objXmlHttp.status
responseText = objXmlHttp.responseText
The code is sending a POST. POST is from HTTPS to HTTPS URL.
When executing this post in the beginning I sometimes received a timeout, so in turn I upped the Timeouts until I am not receiving any timeouts anymore. But now I am sometimes receiving this error:
WinHttp.WinHttpRequest: The server returned an invalid or unrecognized response
When I resend the POST request then it works again. Is there any way to stop this errors?
Before the winHTTP type I always used the MSXML2.XMLHTTP object, but because TLS <1.2 will be depreceted for us, I switched to the winHTTP but this sometimes gives problems.
Token = aiudhaiw
Target = 88192823
url = "https://example.com" + Token + Target
Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.Open "GET", url, False
o.Send
Lets say I have this code for example, it works, but it only works 1 time. What I'm trying to do is to update Token or Target or both (if needed) in the next lines and then use o.Send again and it should send it again, and it should work right? But it doesn't.
if I try with the same Token, Target again, it doesn't work. Even if I change them, and change url again, it doesn't work.
The only way that I can get it to work is to make new o.Open, and it also doesn't work with the same Token and Target again.
Calling Send on an open request twice results in an error 0xC00C0240:
This method cannot be called after the send method has been called.
So you MUST open a new request before you can call Send again.
Demonstration:
This works:
>>> url = "https://www.example.org/"
>>> Set req = CreateObject("Msxml2.XMLHTTP.6.0")
>>> req.Open "GET", url, False
>>> req.Send
>>> req.Open "GET", url, False
>>> req.Send
This doesn't:
>>> url = "https://www.example.org/"
>>> Set req = CreateObject("Msxml2.XMLHTTP.6.0")
>>> req.Open "GET", url, False
>>> req.Send
>>> req.Send
This method cannot be called after the send method has been called.
(0xC00C0240)
Also, to change the URL you must re-open the request. The URL on an already open request cannot be changed.
>>> url = "https://www.example.org/"
>>> Set req = CreateObject("Msxml2.XMLHTTP.6.0")
>>> req.Open "GET", url, False
>>> req.Send
>>> url = "https://www.example.com/" 'this has no effect on req!
>>> req.Send
This method cannot be called after the send method has been called.
(0xC00C0240)
>>> req.Open "GET", url, False 'now the request uses the new URL
>>> req.Send
Whether a token can be re-used depends entirely on how the request is processed by the server. If a token is for one-time use only you cannot expect to be able to successfully send the same request twice.
I'm making a call to an external API. The data it returns, annoyingly, is in the header (the text response is empty).
How do I access the header of the response?
This is what I'm trying:
Dim httpRequest, postResponse
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP")
httpRequest.Open "POST", "http://www.api.com", False, "un", "pw"
httpRequest.SetRequestHeader "Content-Type", "application/json"
httpRequest.setRequestHeader "Content-Length", len(jsondata)
httpRequest.Send data
if httpRequest.status = 200 then
response.write httpRequest.getResponseHeader
response.write httpRequest.ResponseText
end if
Set httpRequest = nothing
But it gives me:
msxml3.dll error '80072f76'
The requested header was not found
And a bonus question: I just noticed the "XML" part of "MSXML2.ServerXMLHTTP" - am I using the right protocol? It's always worked for straight posts and gets until now.
You need to specify the name of the response header you want to retrieve:
response.write httpRequest.getResponseHeader("SomeHeaderName")
There's no just one response header. There could be many. You have the standard response headers such as Content-Type and you could also have custom headers.
And a bonus question: I just noticed the "XML" part of
"MSXML2.ServerXMLHTTP" - am I using the right protocol?
Yes, absolutely, that's the correct COM object to be used from a classic ASP application to send HTTP requests.
I would like to use the Pingdom REST API from Classic ASP, but the following code:-
' setup the URL
baseUrl = "https://api.pingdom.com/api/2.0/checks"
' setup the request and authorization
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "GET", baseUrl, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "userpwd", "aaaaaaaaaaaaaaaaaaaaaaa:bbbbbbbbbbb"
http.setRequestHeader "App-Key", "ccccccccccccccccccccccccccccccc"
' send the HTTP data
http.send
gives me the error:-
{"error":{"statuscode":401,"statusdesc":"Unauthorized","errormessage":"User credentials missing"}}
so my authentication isnt being passed correctly, and it looks like it should NOT be passed in the requestheader, but I'm not sure how it should be done.
Thanks
Thanks for Alex K. and for the benefit of others, the correct syntax is:-
' setup the URL
baseUrl = "https://api.pingdom.com/api/2.0/checks"
Response.Write fullUrl
' setup the request and authorization
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "GET", baseUrl, False, "emailaddress", "password"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "App-Key", "keykeykeykeykeykeykeykeykeykey"
' send the HTTP data
http.send
:-)
https://api.pingdom.com/api/2.0/checks is using Basic Authentication so you need to pass your credentials in the .open call.
I'm using Microsoft.XMLHTTP to get some information from another server from an old ASP/VBScript site. But that other server is restarted fairly often, so I want to check that it's up and running before trying to pull information from it (or avoid my page from giving an HTTP 500 by detecting the problem some other way).
How can I do this with ASP?
You could try making a ping to the server and check the response.
Take a look at this article.
All you need to do is have the code continue on error, then post to the other server and read the status from the post. Something like this:
PostURL = homelink & "CustID.aspx?SearchFlag=PO"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.3.0")
on error resume next
xmlhttp.open "POST", PostURL, false
xmlhttp.send ""
status = xmlhttp.status
if err.number <> 0 or status <> 200 then
if status = 404 then
Response.Write "ERROR: Page does not exist (404).<BR><BR>"
elseif status >= 401 and status < 402 then
Response.Write "ERROR: Access denied (401).<BR><BR>"
elseif status >= 500 and status <= 600 then
Response.Write "ERROR: 500 Internal Server Error on remote site.<BR><BR>"
else
Response.write "ERROR: Server is down or does not exist.<BR><BR>"
end if
else
'Response.Write "Server is up and URL is available.<BR><BR>"
getcustomXML = xmlhttp.responseText
end if
set xmlhttp = nothing