Java and. Net WebServices call each other

Source: Internet
Author: User

 

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
WindowsXP IIS eclipse3.1 (Tomcat axis installed) vs. NET 2003 remarks: download and install the tomcat5.5.16 and Axis Development kits and configure them accordingly. Read my previous document. Iii. Use. NET as a client to call Web Services written in Java
1. Generate a Java Web Services
Using eclipse to generate a Web Service is very simple. Create a new web project, right-click to create a class named helloclient. java. Code The Java code is as follows:
    1. Public class helloclient
    2. {
    3. Public String getname (string name)
    4. {
    5. Return "hello," + name;
    6. }
    7. }

Right-click this file and choose "WebServices" --> "Create web service" to deploy a ws on the local machine. Enter http: // localhost: 8008/testwebservice/services/helloclient In the IE browser to view the deployed helloclient service. Enter http: // localhost: 8008/testwebservice/services/helloclient? The description file of the service.

Create a project (either winform or Asp.net). Here I use an ASP. NET project. Enter a text input box on the start page, a tag to display the results of Web Services calling, and a button to click to call Web Services. Then, select Add web reference. Fill in http: // localhost: 8008/testwebservice/services/helloclient in the URL column? Enter javawebservice as the Web reference name and click Add reference. In this case, we can see this web reference in solution Explore of vs.net.
Add the following code:

C # code
    1. Protected system. Web. UI. webcontrols. textbox txtname;
    2. Protected system. Web. UI. webcontrols. Button btnsend;
    3. Protected system. Web. UI. webcontrols. Label lblmsg;
    4. Private javawebservice. helloclientservice ws;
    5. Private void page_load (Object sender, system. eventargs E)
    6. {
    7. // Place user code here to initialize the page
    8. Ws = new testwebservice. javawebservice. helloclientservice ();
    9. }
    10. Private void btnsend_click (Object sender, system. eventargs E)
    11. {
    12. Lblmsg. Text = ws. getname (txtname. Text );
    13. }

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

1. Use vs. Net to create an ASP Web Services Project and add a web service named service1.asmx. Add a web method with the following code:

C # code
    1. // Specify soapaction
    2. [Soaprpcmethod (Action = "http://www.zjh.com/Rpc", requestnamespace = "http://www.zjh.com/SU", responsenamespace = "http://www.zjh.com/SU")]
    3. [Webmethod]
    4. Public String getname (string name)
    5. {
    6. Return "hello" + name;
    7. }

Then run it and use IE for testing.

2. Open eclipse, create a web project, add a JSP page, name it action_hello.jsp, and enter the following code:

Java code
    1. <% @ Page pageencoding = "GBK" contenttype = "text/html; charset = GBK" %>
    2. <% @ Page import = "org. Apache. axis. Client. Call" %>
    3. <% @ Page import = "org. Apache. axis. Client. Service" %>
    4. <% @ Page import = "javax. xml. namespace. QNAME" %>
Java code
  1. <%
  2. Request. setcharacterencoding ("GBK ");
  3. String name = request. getparameter ("name ");
  4. String endpoint = "http: // localhost/helloclient/service1.asmx ";
  5. Service = new service ();
  6. Call call = (CALL) service. createcall ();
  7. Call. settargetendpointaddress (New java.net. URL (endpoint ));
  8. Call. setoperationname (New QNAME ("http://www.zjh.com/SU", "getname "));
  9. Call. addparameter ("name", org. Apache. axis. encoding. xmltype. xsd_date, javax. xml. rpc. parametermode. In );
  10. Call. setreturntype (Org. Apache. axis. encoding. xmltype. xsd_string );
  11. Call. setusesoapaction (true );
  12. Call. setsoapactionuri ("http://www.zjh.com/Rpc ");
  13. String res = (string) Call. Invoke (new object [] {name });
  14. Out. Print (RES );
  15. %>

This is more complicated than Java calls ws written in Java. Both WS and Java clients must set soapaction; otherwise, system. Web. Services. Protocols. soapexception may occur: the server fails to recognize the value of soapaction in the HTTP header.

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. See my next article.Article.

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.