Consuming a Java Webservice in .Net
February 24th, 2006
hi!! guys, yesterday i developed a simple “Hello World!” webservice with java. With Netbeans 5.0 IDE its a breeze to create web services. It comes bundled with Apache Tomcat 5.5.9 and Sun Java Application Server 8.2.
You can find the quickstart tutorial here to develop web services. Just follow that tute and you will be fine. I will tell you how to consume the same using .net. I used Visual Studio 2005 to consume this service. I assume that you have the url for the wsdl of the webservice which should be similar to, http://localhost:8080/WebApplication1/NewWebService?WSDL.
1. Open Visual studio. “File->New->Website”. If using VS2003 then, “File->New->Project->ASP.NET Web Application”.
2. A web application should have been created by now and you have the “design view” of “default.aspx” or any other aspx page of your choice.
3. Press Ctrl+Alt+L or View->Solution Explorer. Right click on your project name and select “Add Web Reference”. In the windows that pops up, give the WSDL url and an “Instance Name”. My instance name was “JavaAdd”.
4. Add a button and a label to the asp.net page. Double click the button and paste the following code.
VB.NET
Dim js As New JavaAdd.NewWebService()
Dim myadd As New JavaAdd.add()
Dim myaddRes As New JavaAdd.addResponse()
myadd.int_1 = 10
myadd.int_2 = 10
myaddRes = js.add(myadd)
Label1.Text = myaddRes.result
C#
JavaAdd.NewWebService js = new JavaAdd.NewWebService();
JavaAdd.add myadd = new JavaAdd.add();
JavaAdd.addResponse myaddRes=new JavaAdd.addResponse(); myadd.int_1=10;
myadd.int_2=10;
myaddRes=js.add(myadd); Label1.Text=Convert.ToString(myaddRes.result);
Instead of “sayHi” method in the quickstart tute, i created a method called “add” which accepts 2 integer parameters and returns an integer after adding them up.
5. Press F5 now. You should get something similar to these.
Thats it for this post…bye!