Java and. NET, WebServices call each other



This article includes two parts, the first describes how to use. NET client calls made to write Java Web Services, the second section describes how to use Java to do the client call. NET Development Web Services.

Project tools needed

◆ WindowsXP Professial (IIS)

◆ Eclipse3.1 (already installed Tomcat axis)

◆ VS.Net 2003

Note: Download and install the development package Tomcat5.5.16 and Axis, and make the appropriate configuration.See my previous piece of documentation.

Used. NET client calls made to write Java Web Services

1. Generate a JavaWeb Services

Generate a Web Services with Eclipse is very simple.Create a Web project, right to create a Class, named HelloClient.java.Code:

Java code:

public class HelloClient {public String getName (String name) {return "Hello," + name;}}

Right-select the file "WebServices "-->" Create Web Service" to deploy a native ws.In the IE browser, enter the http://localhost:8008/testWebService/services/HelloClient can see the HelloClient services have been deployed.Enter http://localhost:8008/testWebService/services/HelloClient?wsdl the service description file.

Create a new project (WinForm, ASP.net can), I used here is a Asp.Net project.Placed in the start page of a text input box to input a label to display the results of Web Services calls, place a button to click called Web Services.Then, select Add Web Reference, URL of a column filled http://localhost:8008/testWebService/services/HelloClient?wsdl, Web reference to the name of the input JavaWebService, click Add Reference button on it.At this point, we can see VS.net the Solution Explore the Web reference.Add the following code:

C # code:

protected System.Web.UI.WebControls.TextBox txtName; protected System.Web.UI.WebControls.Button btnSend; protected System.Web.UI.WebControls.Label lblMsg; private JavaWebService.HelloClientService ws; private void Page_Load (object sender, System. EventArgs e) {/ / Put user code to initialize the page ws = new testWebService.JavaWebService.HelloClientService ();} private void btnSend_Click (object sender, System.EventArgs e) {lblMsg.Text = ws.getName(txtName.Text);}

Thus, a. NET client to complete, test, work normal, OK.

To do with the Java client calls. NET Web Services written in

1. Using VS.NET create a new Asp Web Services project, add a web service named Service1.asmx.Add a web method, the code is as follows:

C # code:

/ / Specify SoapAction [SoapRpcMethod (Action = "http://www.xxx.com/Rpc", RequestNamespace = "http://www.xxx.com/SU", ResponseNamespace = "http://www.xxx.com / SU ")] [WebMethod] public string getName (string name) {return" Hello "+ name;}

Then run it and use IE to test a success.

2. Open Eclipse, create a new Web project, add a JSP page, named action_hello.jsp, enter the following code:

Java code:

java code

The write than Java calling Java ws to the complex.ws and Java client must set the SOAPAction, or there will be exceptions that System.Web.Services.Protocols.SoapException: server failed to recognize the value of HTTP Header SOAPAction

Summary

After two days of tests, and finally clear from the technical implementation through Web Services. Net and java interoperability is possible.Of course, there are several points to note:

1. In the provision of Web Services, try to use the xml schema to support the type of variables as parameters.If you are using. Net in the dataset of this type, parsing it for the java, it would be a disaster, of course, in theory, can be resolved.But from an efficiency point of view, the Web Services to exchange information with the client in the process, there is always a serialization and deserialization problem.If you use a dataset of this type, the system also need to serialize it to operate, it will be a very expensive process.The string type will be much simpler to use.

2. If you use the soap header and other extended features, such as Microsoft's WSE uses technology to communicate between them need special treatment.