It is easy to import and use SOAP webservice with web reference facility in Visual Studio, however setting SOAPAction needs an extra work on webreference libraries specially when using third party Java provided services. The method used in my class can be used as an alternative to Visual Studio automatic web reference generator. I also add a SOAP header and generate request manually in XML level.
Using the Code
The following class has only one function that creates an XML request with SOAP message structures of a ticket reservation including 5 detailed reservation fields filled at client node and sends a SOAP request using .NET
HttpWebRequest POST
method. In my example, I have to use a chain of characters as SOAPaction in Header (generated by Java Magic Platform at Server side).
Before using this class, you must first generate your XML Request message provided by your webservice provider (usually a WSDL URL or in my case a DLL URI). If easier for you, can also download use an XML SOAP Request creator such as Altova XMLspy at:
http://www.altova.com/download.html[
^]
After generating XML request format, you can include the following class into your application and simply call the following
static
method with your service provider URL address and Proxy:
Dim string As ResultinXMLString = clsCustomizedSOAP.HttpSOAPRequest("http://150.8.111.69/Magic94Scripts/mgrqispi94.dll", Nothing)
Class Code:
Imports Microsoft.VisualBasic
Imports System.Net
Imports System.IO
Imports System
Imports System.Xml
Public Class clsCustomizedSOAP
Public Shared Function HttpSOAPRequest(ByVal WebServiceURL As String, ByVal proxy As String) As String
Dim docXML As XmlDocument = New XmlDocument()
Dim docXmlstr As String
docXmlstr = "<"
docXmlstr = docXmlstr + "SOAP-ENV:Envelope xmlns:SOAP-ENV=" + Chr(34)
docXmlstr = docXmlstr + "http://schemas.xmlsoap.org/soap/envelope/" + Chr(34)
docXmlstr = docXmlstr + " xmlns:SOAP-ENC=" + Chr(34)
docXmlstr = docXmlstr + "http://schemas.xmlsoap.org/soap/encoding/" + Chr(34)
docXmlstr = docXmlstr + " xmlns:xsi=" + Chr(34)
docXmlstr = docXmlstr + "http://www.w3.org/2001/XMLSchema-instance" + Chr(34)
docXmlstr = docXmlstr + " xmlns:xsd=" + Chr(34)
docXmlstr = docXmlstr + "http://www.w3.org/2001/XMLSchema" + Chr(34) + ">"
docXmlstr = docXmlstr + "<"
docXmlstr = docXmlstr + "SOAP-ENV:Body>"
docXmlstr = docXmlstr + "<m:reservationimport xmlns:m=" + Chr(34) + " companyreservation=" + Chr(34) + ">"
docXmlstr = docXmlstr + "<m:referanceno>134452-INT</m:referanceno>"
docXmlstr = docXmlstr + "<m:reservationbranch>Branch No 1</m:reservationbranch>"
docXmlstr = docXmlstr + "<m:outdate>27 Sept 2011 11:00:000 PM</m:outdate>"
docXmlstr = docXmlstr + "<m:expirydate>29 Sept 2011 12:00:000 PM</m:expirydate>"
docXmlstr = docXmlstr + "<m:remarks>Just a comment!</m:remarks>"
docXmlstr = docXmlstr + "</m:reservationimport>"
docXmlstr = docXmlstr + "<" + "/SOAP-ENV:Body" + ">"
docXmlstr = docXmlstr + "<" + "/SOAP-ENV:Envelope" + ">"
Dim retVal As String = ""
Try
docXML.InnerXml = docXmlstr
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(WebserviceURL), HttpWebRequest)
If Not (proxy Is Nothing) Then
req.Proxy = New WebProxy(proxy, True)
End If
req.Headers.Add("SOAPAction", "56369667275635265675E6F6964716672756375625F2265675F6270747E65625A336F646")
req.KeepAlive = False
req.Method = "POST"
req.ContentType = "text/html"
Dim stm As Stream = req.GetRequestStream()
docXML.Save(stm)
stm.Close()
Dim resp As WebResponse = req.GetResponse()
stm = resp.GetResponseStream()
Dim r As StreamReader = New StreamReader(stm)
retVal = r.ReadToEnd
Catch ex As Exception
Throw ex
End Try
Return retVal
End Function
End Class
That's it!