{"id":13,"date":"2009-02-22T04:09:44","date_gmt":"2009-02-22T08:09:44","guid":{"rendered":"http:\/\/blog.halnesbitt.com\/2009\/02\/22\/simple-soap-exchange-in-aspnet\/"},"modified":"2016-09-01T00:04:51","modified_gmt":"2016-09-01T04:04:51","slug":"simple-soap-exchange-in-aspnet","status":"publish","type":"post","link":"https:\/\/halnesbitt.com\/blog\/2009\/02\/22\/simple-soap-exchange-in-aspnet\/","title":{"rendered":"Simple SOAP Exchange in ASP.NET"},"content":{"rendered":"<p><!--more--><br \/>\n**UPDATE 7\/22\/2010** &#8211; So I learned that it is NOT a good idea to use MSXML in ASP.NET applications and it can create a bunch of errors, so the orginal code at the bottom is now crap, but I will keep it up, just for reference. What I did is go through my site and utilize the HttpWebRequest\/HttpWebResponse method for communicating with the web service &#8211; much more efficient. Below is a code sample for ASP.NET 1.1 for sending the initial request\/response to get the token, which can then be used to additional requests for more info:<\/p>\n<pre>\r\n<code>\r\n&lt;%@ Page Language=\"VB\" %&gt;\r\n&lt;%@ Import Namespace=\"System\" %&gt;\r\n&lt;%@ Import Namespace=\"System.IO\" %&gt;\r\n&lt;%@ Import Namespace=\"System.Net\" %&gt;\r\n&lt;%@ Import Namespace=\"System.Xml\" %&gt;\r\n&lt;script runat=\"server\" language=\"VB\"&gt;\r\n\r\nSub Page_Load(sender As Object, e As EventArgs)\r\n\r\n'------------------------------------------------------------------------------------------------------\r\n'setting up variables\r\n'-----------------------------------------------------------------------------------------------------\r\nDim avuser, avpassword As String\r\navuser = \"yourwebserviceusername\"\r\navpassword = \"yourwebservicepassword\"\r\n\r\nDim url As String = \"http:\/\/yourwebserviceurl\"\r\nDim therequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)\r\n\r\n\r\n'----------------------------------------------------------------------------\r\n'creating the XML string\r\n'----------------------------------------------------------------------------\r\n\r\nDim strSoap As String\r\n\r\n\tstrSoap = \"&lt;?xml version=\"\"1.0\"\" encoding=\"\"utf-8\"\"?&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"&lt;soap:Envelope xmlns:soap=\"\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"\" \"\r\n\tstrSoap = strSoap & \"\txmlns:xsi=\"\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\" \"\r\n\tstrSoap = strSoap & \"\txmlns:xsd=\"\"http:\/\/www.w3.org\/2001\/XMLSchema\"\"&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"  &lt;soap:Body&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"    &lt;Authenticate xmlns=\"\"http:\/\/www.avectra.com\/2005\/\"\"&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"      &lt;userName&gt;\" & avuser & \"&lt;\/userName&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"      &lt;password&gt;\"& avpassword & \"&lt;\/password&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"    &lt;\/Authenticate&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"  &lt;\/soap:Body&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"&lt;\/soap:Envelope&gt;\" & vbCrLf\r\n\r\n\r\n'----------------------------------------------------------------------------\r\n'Sending the request\r\n'----------------------------------------------------------------------------\r\n\r\nDim encoding As New ASCIIEncoding()\r\n\r\nDim bytesToWrite As Byte() = encoding.GetBytes(strSoap)\r\n\r\ntherequest.Method = \"POST\"\r\ntherequest.ContentLength = bytesToWrite.Length\r\ntherequest.Headers.Add(\"SOAPAction: \"\"http:\/\/www.avectra.com\/2005\/Authenticate\"\"\")\r\ntherequest.ContentType = \"text\/xml; charset=utf-8\"\r\n\r\nDim newStream As Stream = therequest.GetRequestStream()\r\nnewStream.Write(bytesToWrite, 0, bytesToWrite.Length)\r\nnewStream.Close()\r\n\r\n'----------------------------------------------------------------------------\r\n'Catching the response\r\n'----------------------------------------------------------------------------\r\n\r\nDim theresponse As HttpWebResponse = DirectCast(therequest.GetResponse(), HttpWebResponse)\r\nDim dataStream As Stream = theresponse.GetResponseStream()\r\nDim reader As New StreamReader(dataStream)\r\n\r\nDim responseFromServer As String = reader.ReadToEnd()\r\n\r\ntheresponse.Close()\r\ndataStream.Close()\r\nreader.Close()\r\n\r\n\r\n'----------------------------------------------------------------------------\r\n'Turn the response into an XML document and parse it\r\n'----------------------------------------------------------------------------\r\n\r\nDim XMLDocument = New XmlDocument\r\nXMLDocument.LoadXML(responseFromServer)\r\n\r\nDim authXML As XmlNodeList\r\nDim authNode As XmlNode\r\nDim getToken As XmlNode\r\nauthXML = XMLDocument.GetElementsByTagName(\"AuthorizationToken\")\r\n\r\nDim avtoken As String\r\n\r\n  For Each authNode In authXML\r\n    getToken = authNode.FirstChild\r\n    avtoken = getToken.InnerText\r\n  Next\r\n'--------------------------------------------------------------------------------------\r\n'sending the next call to get the customer key\r\n'--------------------------------------------------------------------------------------\r\n'here you would enter the code for the next call (with the token) to get the additional info.\r\n'and so on and so forth...........\r\n\r\n&lt;\/script&gt;\r\n<\/code>\r\n<\/pre>\n<p>******THIS IS THE OLD POST AND IS ONLY KEPT FOR INFORMATIONAL PURPOSES &#8211; DO NOT USE THE CODE BELOW IN AN ASP.NET SITE******<\/p>\n<p>I am very new to SOAP and the whole web services stuff, but I needed to develop an application that would grab some info from our database and manipulate it. We run an ASP.NET site and use Avectra netFORUM for our database and although the Avectra wiki had some nice examples, I wanted to do it from a simple aspx page. However, although there is tons of info out there on web services and how to consume them in ASP.NET, I could not find any that were simple enough for what I needed. I just wanted to run the exchange from a simple page, rather than compiling the whole website. I was able to convert the asp example from the wiki to aspx and I thought I would share. The methods for your web services will differ, but you ought to be able to get the gist. For those that are new (as I am), basically you just send a SOAP call as XML to the service to get a token, capture the token in a variable, and then send it back, along with the method you want to invoke and it will return the information requested. Check it:<\/p>\n<pre>\r\n<code>\r\n&lt;%@ Page Language=\"VB\" %&gt;\r\n&lt;script runat=server language=VB&gt;\r\n'~~~~~~~~~~~SOAP app by Hal Nesbitt~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n'This app grabs a member number and then returns basic information about that person from the database\r\n\r\nSub Page_Load\r\n\r\n\r\n'----------------------------------------------------------------------------------------\r\n'Define your database\/web services variables\r\nConst avURL As String = \"http:\/\/yourwebservicesurl.asmx\"\r\n\r\nDim user As String\r\nDim password As String\r\nDim host As String\r\nDim authenticate As String\r\nDim action As String\r\nuser = \"yourusername\"\r\npassword = \"yourpassword\"\r\nhost = \"yourhosturl\"\r\nauthenticate = \"yourauthenticateaction\"\r\naction = \"yourwebserviceaction\"\r\n\r\n'-----------------------------------------------------------------------------------------\r\n\r\n'This is the membernumber of the person you are trying to lookup. It is passed in a querystring\r\nDim membernumber As String\r\nmembernumber = Request.QueryString(\"membernumber\")\r\n\r\n\r\n'Setting initial variables\r\nDim xmlhttp As Object \r\nDim strSoap As String \r\nDim xmlFile As Object \r\nDim str As String \r\nDim strResponse As String\r\nDim xmlDoc As Object\r\nDim oNode As String\r\nDim xmloutput As String\r\nDim token As String\r\nDim ASNID As String\r\nDim ASNPage As String\r\n\r\n'This creates the xml that will be sent as a soap call to get the initial token\r\n\r\n\tstrSoap = \"&lt;?xml version=\"\"1.0\"\" encoding=\"\"utf-8\"\"?&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"&lt;soap:Envelope xmlns:soap=\"\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"\" \"\r\n\tstrSoap = strSoap & \"\txmlns:xsi=\"\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\" \"\r\n\tstrSoap = strSoap & \"\txmlns:xsd=\"\"http:\/\/www.w3.org\/2001\/XMLSchema\"\"&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"  &lt;soap:Body&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"    &lt;Authenticate xmlns=\"\"http:\/\/www.avectra.com\/2005\/\"\"&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"      &lt;userName&gt;\" & user & \"&lt;\/userName&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"      &lt;password&gt;\"& password & \"&lt;\/password&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"    &lt;\/Authenticate&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"  &lt;\/soap:Body&gt;\" & vbCrLf\r\n\tstrSoap = strSoap & \"&lt;\/soap:Envelope&gt;\" & vbCrLf\r\n\r\n' uses \"Msxml2.ServerXMLHTTP\" to allow the sending of the credentials \r\n' which \"Msxml2.XMLHTTP\" won't do without a lot of screwing around \r\nxmlhttp = CreateObject(\"Msxml2.ServerXMLHTTP\") \r\nxmlhttp.open(\"POST\", avURL, false) \r\nxmlhttp.setRequestHeader (\"Host\", host)\r\nxmlhttp.setRequestHeader (\"Content-type\", \"text\/xml; charset=utf-8\")\r\nxmlhttp.setRequestHeader (\"Content-Length\", len (strSoap)+1)\r\nxmlhttp.setRequestHeader (\"SOAPAction\", authenticate)\r\n\r\n' send the request to the web service \r\nxmlhttp.send(strSoap) \r\n\r\n'Show what SOAP I am sending. Use this for debugging\r\n'Response.Write(strSoap)\r\n\r\n' capture the SOAP response\r\n\tstrResponse = xmlhttp.responseText\r\n \r\n\t' load response into xml DOM object\r\n\txmlDoc = Server.CreateObject(\"Microsoft.XMLDOM\")\r\n\txmlDoc.async=\"false\"\r\n\txmlDoc.loadXML(strResponse)\r\n\r\n\r\n \r\n\t' get token if one was returned\r\n\toNode = xmlDoc.selectSingleNode(\"soap:Envelope\/soap:Header\/AuthorizationToken\/Token\").Text\r\n\tIf Not oNode Is Nothing Then token = xmlDoc.selectSingleNode(\"soap:Envelope\/soap:Header\/AuthorizationToken\/Token\").Text\r\n\t \r\n\r\n\r\n\r\n'Shows the SOAP Response. Use this for debugging\r\n'Response.Write(strResponse)\r\n\r\n\r\n'Now we have the Authorization token. Below gives the status\r\n\r\n' check the status the web service returned \r\nIf xmlhttp.status = \"200\" Then \r\n' we're good to go \r\nResponse.Write(\"Good to go. Status is \")\r\nResponse.Write(xmlhttp.status & \"&lt;br \/&gt;\")\r\nResponse.Write(\"Authorization Token: \" & avtoken)\r\nElse \r\n' there's a problem \r\nResponse.Write(\"shit ain't no good\")\r\nResponse.Write(xmlhttp.status)\r\nEnd If \r\n\r\n'-------------------------------------------------------------------------------\r\n\r\n'Now we take the token and send it back to the web service to get some customer info from the supplied membernumber\r\n'This uses the WEBWebUserGetByRecno_Custom Web Service\r\n' concatenate xml string\r\nstrSoap = \"&lt;?xml version=\"\"1.0\"\" encoding=\"\"utf-8\"\"?&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"&lt;soapenv:Envelope xmlns:soapenv=\"\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"\" xmlns:ns=\"\"http:\/\/www.avectra.com\/2005\/\"\"&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"   &lt;soapenv:Header&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"      &lt;ns:AuthorizationToken&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"         &lt;ns:Token&gt;\" & token & \"&lt;\/ns:Token&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"      &lt;\/ns:AuthorizationToken&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"   &lt;\/soapenv:Header&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"   &lt;soapenv:Body&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"      &lt;ns:WEBWebUserGetByRecno_Custom&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"         &lt;ns:cst_recno&gt;\" & membernumber & \"&lt;\/ns:cst_recno&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"      &lt;\/ns:WEBWebUserGetByRecno_Custom&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"   &lt;\/soapenv:Body&gt;\" & vbCrLf\r\nstrSoap = strSoap & \"&lt;\/soapenv:Envelope&gt;\" & vbCrLf\r\n\r\n\r\nxmlhttp = CreateObject(\"Msxml2.ServerXMLHTTP\") \r\nxmlhttp.open(\"POST\", avURL, false) \r\nxmlhttp.setRequestHeader (\"Host\", host)\r\nxmlhttp.setRequestHeader (\"Content-type\", \"text\/xml; charset=utf-8\")\r\nxmlhttp.setRequestHeader (\"Content-Length\", len (strSoap)+1)\r\nxmlhttp.setRequestHeader (\"SOAPAction\", action)\r\n\r\n' send the request to the web service \r\nxmlhttp.send(strSoap) \r\n\r\n'Show what SOAP I am sending. Use this for debugging\r\n'Response.Write(strSoap)\r\n\r\n' capture response\r\n\tstrResponse = xmlhttp.responseText\r\n \r\n\t' load response into xml DOM object\r\n\txmlDoc = Server.CreateObject(\"Microsoft.XMLDOM\")\r\n\txmlDoc.async=\"false\"\r\n\txmlDoc.loadXML(strResponse)\r\n\r\n \r\n\t' get token if one was returned. This is now the new token, in case I want to make subsequent calls to the web service\r\n\toNode = xmlDoc.selectSingleNode(\"soap:Envelope\/soap:Header\/AuthorizationToken\/Token\").Text\r\n\tIf Not oNode Is Nothing Then token = xmlDoc.selectSingleNode(\"soap:Envelope\/soap:Header\/AuthorizationToken\/Token\").Text\r\n\t \r\n\r\n\r\n\r\n'Shows the SOAP Response. Use this for debugging\r\nResponse.Write(\"&lt;br&gt;&lt;br&gt;This is the XML output: &lt;br \/&gt;\" & strResponse)\r\n\r\n'Displays the new token, for subsequent calls\r\nResponse.Write(\"&lt;br&gt;&lt;br&gt;This is the NEW token: &lt;br \/&gt;\" & avtoken)\r\n\r\n\r\nEnd Sub\r\n\r\n'-----------------------------------------------------------------------------------\r\n\r\n'This handles errors. If a membernumber is invalid, then it will redirect to an error page.\r\n\r\nPrivate Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)\r\nDim errorpage As String\r\n\r\nResponse.Redirect(\"http:\/\/yoursite\/error.aspx\")\r\n    \r\nEnd Sub\r\n\r\n&lt;\/script&gt;\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-computer-stuff","category-show-all"],"_links":{"self":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":8,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":333,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/333"}],"wp:attachment":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}