Java and. NET Web services call each other

Source: Internet
Author: User
Tags date copy dateformat integer tostring web services java web wsdl
Services|web One: Introduction

This article describes the technologies that Java and. NET development Web services call each other. This article includes two parts, the first part describes how to use. NET do client invoke Java write Web services, the second part describes how to use Java to do client calls. NET development Web services.



II: The tools needed for the project

Windows2000 Server (IIS)

Jbuilder9.0 (contains tomcat, axis)

Jdk1.4+java Web Services Develop

Vs.net 2003

Note: If you do not have JBuilder, you will need to download and install Tomcat4.1 and axis development kits and configure them accordingly. Since there are many such articles on the Internet, this is not explained here.



Three: use. NET makes the client invoke Java write Web Services

1. Generate a Java Web Services

Using JBuilder to build a Web service is very simple, and I am doing it with all my help, just one step at a

Go down on it. The specific path is developing Web services-> Tutorials:web Services (Axis)-> creating a simple Web services

2. Publish this Web services and get its WSDL

When Web Services is built, F9 runs the project. Then, click View to browse through the existing Web service, click the Bean1 WSDL connection, and we can view its WSDL description in the browser. Copy the WSDL address in the browser address bar for next use. NET development client to invoke the program.

3. Generate a. NET client with Vs.net

Create a new item (Winform,asp.net can), I'm using a asp.net project here. Place a text input box on the start page to display the results of calling Web services, and place a button to click the Call Web Services. Then, choose to add a Web reference, copy the WSDL address that you just obtained in the WSDL column, enter the name of the Web reference Javaservice, and click the Add Reference button. At this point, we can see this Web reference in Vs.net's solution explore.

Enter the following code in the Click event of the button:

Javaservice.bean1 bean = new Javaservice.bean1 ();

TextBox1.Text = Bean.getSample.ToString ();



So, one. NET client is finished, test, work properly, OK.



Four: Use Java to do client call. NET Write Web Services

With the above success, so I thought that using Java to do client calls is also a very easy thing, but the actual situation has cost me two days to achieve.

1. Create a new ASP Web services project with Vs.net, add a Web service, and name it sumservice.asmx. Add a new Web method with the following code:

[WebMethod]

public int Intadd (int a,int b)

{

return a+b;

}

Then run it and use IE to test the success.

2. Open jbuilder9.0, create a new project, add a Java class, name Testnetservice, and enter the following code:

Package mywebservicejavaclient;

Import Java.util.Date;

Import Java.text.DateFormat;

Import Java.util.Date;

Import Java.text.DateFormat;

Import Org.apache.axis.client.Call;

Import Org.apache.axis.client.Service;

Import Javax.xml.namespace.QName;

Import Java.lang.Integer;

Import Javax.xml.rpc.ParameterMode;



/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright:copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/



public class Testnetservice {

Public Testnetservice () {

}



public static void Main (string[] args) {

try {

Integer i = new integer (1);

Integer j = new Integer (2);

String endpoint= "Http://localhost/MyServices/WebServiceTest/SumService.asmx";



Service service = new service ();

Call call = (call) Service.createcall ();



Call.settargetendpointaddress (new Java.net.URL (endpoint));

Call.setoperationname (New QName ("Http://www.my.com/SU", "Intadd"));



Call.addparameter ("A", org.apache.axis.encoding.xmltype.xsd_date,javax.xml.rpc.parametermode.in);

Call.addparameter ("B", org.apache.axis.encoding.xmltype.xsd_date,javax.xml.rpc.parametermode.in);

Call.setreturntype (Org.apache.axis.encoding.XMLType.XSD_INT);

Call.setusesoapaction (TRUE);

Call.setsoapactionuri ("http://www.my.com/Rpc");





Integer k = (integer) call.invoke (New object[]{i,j});



SYSTEM.OUT.PRINTLN ("result is" + k.tostring () + ".");

}

catch (Exception e) {System.err.println (e.tostring ());}

}



}



Running the above Java client program, you will find that the system throws a SOAPAction exception. Strange, how could it be wrong? Thinking about the ASP Web services that I developed did not specify SOAPAction, I added the following code to the previous line of Web method in Sumservice.asmx:

[SoapRpcMethod (action= "http://www.my.com/Rpc", requestnamespace= "Http://www.my.com/SU", responsenamespace= "http ://www.my.com/su ")]

After recompiling the ASP Web services, after executing the Java program, you will find that the output is the correct result 3.

In this way, the Web services that are generated by invoking. NET in Java are also initially completed, as if not very complicated. Actually, in the actual work, I did not find a good code example on the Internet at the beginning, all rely on the help of JBuilder and axis to complete, can always prompt what cannot find corresponding soapaction. In fact, I can see that soapaction in IE browser after running ASP Web services. I have tried to copy the default SOAPAction to Java code, but the Java client still throws the same exception, which I am puzzled.



V: summary

After two days of experimentation, it is technically clear that it is possible to achieve. NET and Java interoperability through Web services. Of course, there are a few points to be noted:

1. When providing Web services, try to use variable types that are supported in XML schemas as parameters. If you use a dataset of. NET, it will be a disaster for Java, and, of course, it can be resolved theoretically. But in terms of efficiency, there is always a problem of serialization and deserialization in the process of exchanging information between Web services and clients. If you use a dataset this type, the system also needs to serialize it, which will be a resource-intensive process. Using the string type is much simpler.

2. If extended features such as SOAP headers are used, such as using the WSE technology provided by Microsoft, the mutual communication between them requires special treatment.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.