Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / ASP
Tip/Trick

How to Invoke Java Webservice using HTTPWebrequest and SOAPAction in .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Oct 2011CPOL1 min read 28.3K   1   1
It is easy to import and use SOAP webservice with web reference facility in Visual Studio, however setting SOAPAction needs a extra work on webreference libraries specially when using third party Java provided services.
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:

VB
Dim string As ResultinXMLString = clsCustomizedSOAP.HttpSOAPRequest("http://150.8.111.69/Magic94Scripts/mgrqispi94.dll", Nothing)  

Class Code:


VB
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' By Amir Oveissian, Sept 2011 									' 
' Introduction: It is easy to import and use SOAP webservice with web reference facility in .NET 		'
' however setting SOAPAction needs a extra work on webreference libraries specially when using 		'
' third party Java provided services. Method used in following class can be used as alternative 			'
' to Visual studio automatic web reference generator. 							' 
'												'
' Operation Detail: 											'
' Following single-method-class 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. 										'
'												'
' Requirement: 											'
' To use 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 			' 
'												'
' Sample of Usage of this class in your VB code: 							'
' clsCustomizedSOAP.HttpSOAPRequest("http://150.8.111.69/Magic94Scripts/mgrqispi94.dll", Nothing) 		'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports Microsoft.VisualBasic
Imports System.Net
Imports System.IO
Imports System
Imports System.Xml
Public Class clsCustomizedSOAP

'Following Function Consumes Java based webservices that need to have SOAPAction on Request header: 

Public Shared Function HttpSOAPRequest(ByVal WebServiceURL As String, ByVal proxy As String) As String

'Generate an XML document:
Dim docXML As XmlDocument = New XmlDocument()	
Dim docXmlstr As String

'To create following SOAP Request XML, you can use Altova XMLSpy or any 	
'other webservice testing/creating application:	

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>"

'ReservationImport is the function name provided by webservice:
docXmlstr = docXmlstr + "<m:reservationimport xmlns:m=" + Chr(34) + " companyreservation=" + Chr(34) + ">"

'Following elements are reservation required information consumed by webservice: 	
docXmlstr = docXmlstr + "<m:referanceno>134452-INT</m:referanceno>" 
'Reservation Reference Number:	
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
		
'Your webservice URL (usually a WSDL web address):		
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(WebserviceURL), HttpWebRequest)

'Set Proxy, only if neccessary:		
If Not (proxy Is Nothing) Then
	req.Proxy = New WebProxy(proxy, True)		
End If

'adding your soap action (in my example, action is a key used by Magic94Scripts platform):

req.Headers.Add("SOAPAction", "56369667275635265675E6F6964716672756375625F2265675F6270747E65625A336F646")

'Not persist connection on service URL:		
req.KeepAlive = False
		
'Using the method POST:		
req.Method = "POST"
		
'In this service, response will be processed as text:		
req.ContentType = "text/html"
		
'write request data in stream:		
Dim stm As Stream = req.GetRequestStream()		
docXML.Save(stm)		
stm.Close()
		
'Return Reponse:		
Dim resp As WebResponse = req.GetResponse()
		
'Process the response returned and return string fomat:		
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Serdar Aytamaner18-Oct-12 3:28
Serdar Aytamaner18-Oct-12 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.