Classic ASP Microsoft.XMLHTTP onreadystatechange - asynchronous

I am trying to make an async call with a callback function in classic ASP. This is my code:
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
With objHTTP
.open "POST", base_url_crm & "contacts", True, login_crm, key_crm
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.onreadystatechange = Check_state
.send json
End with
Set objHTTP = Nothing
Public Function Check_state
if objHTTP.ReadyState = 4 then
Response.Write "Finish"
end if
End Function
Its return this error:
Type mismatch: 'onreadystatechange'
How can I execute the Check_state function right after the async call ends?
It must be async (dont wanna make my users wait) and cant do this in javascript.

You cannot bind events to functions like you do in JavaScript because simply passing the name of the function will call it immediately, return its output, and that'll be assigned instead. Use the GetRef function to bind events like so:
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.onreadystatechange = GetRef("Check_state")
See here for documentation on GetRef.

Try using the status instead of onreadystatechange , it returns values ​​such as 200, 404 and 500.

Related

read all elements from another page using classic ASP

Is there a way to read a another page's elements and grab the whole HTML code?
let's say for example i want to get all the HTML from this page : http://localhost/test3.html
Thank you!
Here's a function I use:
function http_post (strUrl, data)
' call another URL (e.g. ASP or PHP script) via HTTP and POST. "data" should be GET-style parameter string or empty '
dim xmlHttp
Set xmlHttp = Server.Createobject("MSXML2.ServerXMLHTTP.6.0")
With xmlHttp
.Open "POST", strUrl, False
.setRequestHeader "User-Agent", Request.ServerVariables("HTTP_USER_AGENT")
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(data)
.Send data
if .Status < 400 then
http_post = .responseText
else
' do error logging here if you want '
http_post = false
end if
.abort()
End With
set xmlHttp = Nothing
end function

POST Parameters not passing to Web API using VBA

I have a bog standard WebAPI that accepts a POST and takes those parameters and processes certain things. I'm not certain if the problem is the VBA or the ASP.Net WebAPI so I am cross posting.
I've used Postman to test the API and it works fine when I post Key/Value params.
I've also tried the following method and relevant parameters and get the same result:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
I'm having problems getting it to work with the VBA, when debugging in Visual Studio on the WebAPI I cannot see the values I am posting anywhere. It's like the request is coming in blank. My WebAPI is subsequently throwing an error because the parameters are missing and certain parameters are required.
I'm not sure if this is a problem on the VBA side or the ASP.NET WebAPI side so I am cross posting in a hope someone can highlight or spot what I am doing wrong.
Private Sub Command4_Click()
Dim argumentString1 As String
argumentString1 = "companyId=228&secondsToLog=15&subject=TestBackup123&description=TestDescription" & _
"&category=&tag=&ticketType=task&assignee=gavin&requesterEmail=bob#smith.com" & _
"&submitterName=gavin&status=open&priority=normal"
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:64874/api/zendeskhelper"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send (argumentString1)
txtresult = objHTTP.responsetext & ": " & argumentString1
End Sub
The Web API structure looks like this:
public HttpResponseMessage Post([FromUri] TicketBody ticket)
{
// Nothing is bound to ticket like it is in Postman
}
Any help or pointers would be much appreciated!
The way you are sending your data, you need to remove [FromUri]. When using [FromUri], the ASP.NET engine will look for data in the Uri, not the body of the request.
public HttpResponseMessage Post(TicketBody ticket)
{
// 'ticket should not be null now
}
On the other hand, if you need to keep [FromUri] you could change your call to:
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:64874/api/zendeskhelper?" & argumentString1
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send ("")

Getting Partial Response with Asynchronous XMLHTTP

How can I asynchronously read XMLHTTP before it’s finished in VBS? The example
below (which has a ”The data necessary to complete this operation is not yet available”
error if sleep times are not long enough) uses a big Wikipedia page, but ultimately I
am thinking to use this for an infinite webpage/stream that never finishes loading. So,
is there a simple way to modify this example to get partial HTTP responses early?
strFileURL = "https://en.wikipedia.org/wiki/Algorithm" ' or any big webpage
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") ' all of these xmlhttp options have no "streaming success"
' Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
' Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
' Set objXMLHTTP = Createobject("MSXML2.XMLHTTP.3.0")
' Set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
objXMLHTTP.open "GET", strFileURL, true
objXMLHTTP.send()
' Is there something other than ResponseText to avoid error and get partial result below (i.e., read html like a stream)?
WScript.Sleep 500 ' need to play with this time to try to cause mystate1=3 below
mystate1 = objXMLHTTP.ReadyState ' should be 3
myscan = objXMLHTTP.ResponseText ' CAUSES ERROR if mystate1 < 4 (comment this line out to avoid error)
WScript.Sleep 1000
mystate2 = objXMLHTTP.ReadyState ' should be 4
myscan = objXMLHTTP.ResponseText
MsgBox(mystate1)
MsgBox(mystate2)
MsgBox(myscan)
Set objXMLHTTP = Nothing
That's not possible, at least not by using an XMLHTTPRequest. As documented, neither responseText nor responseBody are available before the request is completed.

sendgrid in classic asp add email category

I'm sending mail using sendgrid web API successfully but unable add categories into x-smtpapi.
Here is my code :
function getHTML (strUrl,postData)
Set xmlHttp = Server.Createobject("MSXML2.ServerXMLHTTP")
xmlHttp.Open "POST", strUrl, False
xmlHttp.setRequestHeader "User-Agent", "asp httprequest"
xmlHttp.setRequestHeader "content-type", "application/x-www-form-urlencoded"
'xmlHttp.AddHeader "category", "web"
xmlHttp.Send postData
getHTML = xmlHttp.responseText
xmlHttp.abort()
set xmlHttp = Nothing
end function
Response.Write("test->" & getHTML("https://sendgrid.com/api/mail.send.json","api_user=myusername&api_key=mykey&to=soneone#somemail.com&subject=test-1 msg&html=this is test message&from=info#zyxxxz.com&category={testweb}"))
Response.End()
I've checked some doc here
But I could not find any way to add categories.
EDIT
Response.Write("test->" & getHTML("https://sendgrid.com/api/mail.send.json","api_user=user&api_key=key&to=somemail#somemail.com&subject=test-1 msg&html=this is test message&from=info#xyzzz.com&x-smtpapi={"category":"testCategory"}"))
I need to post it JSON. If I do not put double quote x-smtpapi={"category":"testCategory"}" JSON parser can't parse it!
A double double quote escapes in ASP:
Ex.
#Invalid
str = " She said "Hello World" to the whole group"
#valid
str = " She said ""Hello World"" to the whole group"
So this should work fine:
Response.Write("test->" & getHTML("https://sendgrid.com/api/mail.send.json","api_user=user&api_key=key&to=somemail#somemail.com&subject=test-1 msg&html=this is test message&from=info#xyzzz.com&x-smtpapi={""category"":""testCategory""}"))

vbs xmlhttp responseText Truncated?

Hello all i have a simple vbs script that is grabbing a url(with values using GET) and I need to parse thru the text. However the responsetext i am getting is not the full response. i am getting a string that is 1000 in length however i know the response should be more like 5000.
Function getServer(server_hostname)
Set objHTTP = CreateObject("msxml2.xmlhttp.3.0")
objHTTP.open "GET", "http://someurl/ServerInfo.asp", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send "B1=GO!!&Server=" + server_hostname
getServer = objHTTP.responseText
End Function
serverStr = getServer(server_hostname)
msgbox(Len(serverStr))
Is there a limitation on how much can be returned? Thank you for your help.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So i change my function to:
Function getServer(server_hostname)
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.open "GET", "http://someurl/ServerInfo.asp?B1=GO!!&Server=" + server_hostname , False
'objHTTP.setRequestHeader "Content-Type", "text/html"
'objHTTP.send "B1=GO!!&Server=" + server_hostname
objHTTP.send
getServer = objHTTP.responseText
End Function
And now it works...no idea why.
The maximum MsgBox length is 1024 characters.
Ref: http://www.w3schools.com/vbscript/func_msgbox.asp

Resources