Code Reference A collection of code for my reference (and perhaps other people too)

31May/100

POST using ASP

A more simple way to to a POST. This is cleaner than the last example I created.

I used a nice function at http://www.secretgeek.net/XMLSendReceive.shtml.

' First load the XML to send off
Set SendDoc = server.createobject("Microsoft.XMLDOM")
SendDoc.ValidateOnParse= True
SendDoc.LoadXML(sTrData) 

' Then call the POST function and get a nice object back
set SourceObj = xmlSend (sURL, SendDoc)

Reading the XML:

set oUid = SourceObj.documentElement.selectSingleNode("//UserId")
set oHtml = SourceObj.documentElement.selectSingleNode("//User/Name")

Here is the nifty little function

    private function xmlsend(url, docSubmit)
        Set poster = Server.CreateObject("MSXML2.ServerXMLHTTP")
        poster.open "POST", url, false
        poster.setRequestHeader "CONTENT_TYPE", "text/xml"
        poster.send docSubmit
        Set NewDoc = server.createobject("Microsoft.XMLDOM")
        newDoc.ValidateOnParse= True
        newDoc.LoadXML(poster.responseTEXT)

        Set XMLSend = NewDoc
        Set poster = Nothing
    end function
Tagged as: No Comments
10Sep/092

GET and POST using ASP

In these examples I am calling a WCF service. Many legacy projects out on the net are still using ASP.

POST example with ASP

xmlToSend = "<?xml version=""1.0"" encoding=""utf-8"" ?><InputXMLObject xmlns=""http://schemas.datacontract.org/2004/07/MyService""><FirstName>Darren</FirstName><LangCode>UK</LangCode><LastName>Sullivan</LastName><Address>123 Rue de Nowhere</Address><City>Paris</City></InputXMLObject>"

postUrl = "http://myurl.com/MyService.svc/AddPerson"
Dim HttpReq
Set HttpReq = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
HttpReq.Open "POST", postUrl, False
HttpReq.SetRequestHeader "Content-Type", "application/xml"	'"application/x-www-form-urlencoded"
HttpReq.Send xmlToSend
PostXMLResponse = HttpReq.ResponseText

GET example with ASP

getUrl = "http://MyUrl/MyService.svc/AddPerson?FirstName=Darren&LastName=Sullivan&Address=123 Rue de Nowhere&City=Paris&langCode=UK"

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", getUrl, False
xmlhttp.setRequestHeader "Content-type", "text/xml"
xmlhttp.send()
GetXMLResponse = xmlhttp.responseText

Process the XML result

Set SourceObj = Server.CreateObject("MSXML2.DOMDocument.6.0")
SourceObj.async = false
SourceObj.setProperty "SelectionLanguage", "XPath"
'SourceObj.loadXML( GetXMLResponse ) ' For the GET call
SourceObj.loadXML( PostXMLResponse ) ' For the POST call

Dim NewRecordID, Success, ErrorString

set NewRecordID	= SourceObj.documentElement.selectSingleNode("//RecordID")
set Success = SourceObj.documentElement.selectSingleNode("//Success")
set ErrorString = SourceObj.documentElement.selectSingleNode("//ErrorString")

if not ErrorStringis nothing then
	ErrorText = ErrorString.text
else
	ErrorText = ""
end if
...
Tagged as: 2 Comments
14Aug/090

Checking for null

Checking for null is done differently in each langauge.

* In a string (C#):

if (string.IsNullOrEmpty(myString))
{ }

* In a database (T-SQL):

SELECT *
FROM [Customers]
WHERE Address IS NULL

* Database return value (C#):

if (myProperty == DBNull.Value)
{ }

* In Javascript:

if (myVariable == null)
{ }

* In Javascript - check if an object exists or has been created yet. This is different than checking for a null value:

if (typeof(jQuery) == 'undefined')
{ }

* In ASP:

if myVariable is null then 

end if

* In ASP - check if an object exists - example when receiving an XML object:

Set SourceObj = Server.CreateObject("MSXML2.DOMDocument.6.0")
SourceObj.async = false
SourceObj.setProperty "SelectionLanguage", "XPath"
SourceObj.loadXML( xmlhttp.responseText )

uid = SourceObj.documentElement.selectSingleNode("//sessionId")
if uid is nothing then
   uid = "n.a."
else
   uid = uid.text
end if

* In PHP:



Tagged as: No Comments