Tuesday, November 20, 2007

Best Coding Practices - ASP Post data to URL

Best Coding Practices - ASP Post data to URL


The first time I needed this function it was for posting data to Authorize.net for payment processing but there are many uses. The only caveat is the data sent must be formatted correctly. I've given a simple example of how it's used also.

Here it is:

Code:
Function PostTo(Data, URL)
Dim objXMLHTTP, xml
On Error Resume Next
Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xml.Open "POST", URL, False
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xml.Send Data
If xml.readyState <> 4 then
xml.waitForResponse 10
End If
If Err.Number = 0 AND http.Status = 200 then
PostTo = xml.responseText
else
PostTo = "Failed"
End If
Set xml = Nothing
End Function

PostData = "encoding=UTF-8&firstname=" & firstname & "&lastname=" & lastname & "&email=" & email & ""
rslt = PostTo(PostData,"http://www.myurl.com/formhandler.asp")