I. Introduction
This article introduces the technology of mutual calls between Web services developed by Java and. net. This article consists of two parts: the first part describes how to use. NET as a client to call Web Services written in Java, and the second part describes how to use Java as a client to call web services developed by. net.
Ii. Tools required by the project
Windows2000 Server (IIS)
Jbuilder9.0 (including Tomcat and axis)
Jdk1.4 + Java Web Services develop
Vs. NET 2003
NOTE: If JBuilder is not available, you need to download and install the tomcat4.1 and Axis Development kits and configure them accordingly. Because there are many suchArticle.
Iii. Use. NET as a client to call Web Services written in Java
1. Generate a Java Web Services
Using JBuilder to generate a Web Service is very simple. I do it with its help, as long as I do it step by step. The specific path is developing Web Services-> Tutorials: Web Services (axis)-> creating a simple Web Services
2. Publish this web service and obtain its WSDL
After Web services are generated, F9 runs the project. Then, click View to browse the existing web service and click bean1's WSDL connection. We can view its WSDL description in the browser. Copy the WSDL address in the address bar of the browser and call the. NET development client for the next step.ProgramPrepare.
3. Use vs. Net to generate a. Net client.
Create a project (either winform or Asp.net). Here I use an ASP. NET project. On the start page, place a text input box to display the result of calling Web Services, and place a button to click to call Web Services. Then, select "add web reference" and copy the obtained WSDL address in the "WSDL" column. Enter the Web reference name "erervice" and click "add reference. In this case, we can see this web reference in solution Explore of vs.net.
In the button clicking event, enter the followingCode:
Assumervice. bean1 bean = new assumervice. bean1 ();
Textbox1.text = bean. getsample. tostring ();
In this way, a. Net client is complete. Test it and it works normally. OK.
4. Using Java as a client to call Web Services written by. net
With the above success, I thought it was very easy to use Java for client calls, but it took me two days to implement it.
1. Use vs. Net to create an ASP Web Services Project and add a web service named sumservice. asmx. Add a web method with the following code:
[Webmethod]
Public int intadd (int A, int B)
{
Return A + B;
}
Then run it and use IE for testing.
2. Open Jbuilder9.0, create a project, add a Java class, name it 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 = 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 ());}
}
}
Run the preceding Java client program and you will find that the system throws a soapaction exception. Strange, how can it be wrong? I thought that my asp web services didn't specify soapaction, so 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 the ASP Web Services is re-compiled and run, the correct result 3 is displayed when the Java program is executed.
In this way, the web services generated by using Java to call. NET are also completed. It seems that it is not very complicated, right. In fact, in actual work, I didn't find a good code example on the Internet at the beginning, all of which were completed with the help of JBuilder and axis, it always prompts that no corresponding soapaction can be found. In fact, after running ASP Web Services, I can see the soapaction In the IE browser. I tried to copy the default soapaction to Java code, but the Java client still throws the same exception, which is confusing to me.
V. Summary
After two days of experimentation, it is finally technically clear that it is possible to implement intercommunication between. NET and Java through Web Services. Note the following points:
1. When providing web services, try to use the variable types supported in XML schema as parameters. If the dataset type in. NET is used, it will be a disaster for Java to parse. Of course, it can be parsed theoretically. However, from the perspective of efficiency, there is always a problem of serialization and deserialization in the process of exchanging information between Web Services and clients. If dataset is used, the system also needs to serialize it, which is a resource-consuming process. Using the string type is much simpler.
2. If you use extensions such as SOAP Headers, such as the WSE technology provided by Microsoft, communication between them requires special processing.