Java and. NET WebServices call each other

Source: Internet
Author: User
Tags wsdl

A: Introduction
This article describes the technologies that Java and. NET development Web services call each other. This article consists of two parts, the first part describes how to use. NET as the client calls the Java-written Web services, the second part describes how to use Java to make the client call. NET developed Web services.
II: Tools needed for the project
WindowsXP professial (IIS)

Eclipse3.1 (installed Tomcat axis) vs.net 2003 Note: Download the installation Tomcat5.5.16 and the development kit for axis and configure it accordingly. Look at my previous piece of documentation. Third: using. NET to make client calls to Java-written web Services
1. Generate a Java Web Services
Using Eclipse to build a Web services is very simple. Create a new Web project and right-click to create a class named Helloclient.java. The code is as follows:Java code:public class Helloclient
{
public string GetName (string name)
{
Return "Hello," +name;
}
}

Right-click this file select "WebServices"-"Create Web Service" to deploy to a WS of native. Enter http://localhost:8008/testWebService/services/HelloClient in IE to see the helloclient service already deployed. Enter a description of the HTTP://LOCALHOST:8008/TESTWEBSERVICE/SERVICES/HELLOCLIENT?WSDL service.

Create a new project (Winform,asp.net can), I'm using an ASP. Place a text input box on the start page for input, a label to display the results of the call to Web services, and a button to click Invoke Web Services. Then, choose Add Web Reference, the URL column filled in the name of the Http://localhost:8008/testWebService/services/HelloClient?wsdl,Web reference input Javawebservice, Click the Add Reference button to do so. At this point, we can see this Web reference in Vs.net's solution explore.
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)
{
Place user code here to initialize page
Ws=new TestWebService.JavaWebService.HelloClientService ();
}

private void Btnsend_click (object sender, System.EventArgs e)
{
Lblmsg.text=ws.getname (txtName.Text);
}

So, a. NET client is done, test it, work fine, OK.

Four: Using Java to do client-side calls to. Net-written Web Services

1. Create a new ASP Web services project with Vs.net and add a Web service named Service1.asmx. Add a new Web method with the following code:

C # code:
Specify SOAPAction
[SoapRpcMethod (action= "http://www.zjh.com/Rpc", requestnamespace= "Http://www.zjh.com/SU", responsenamespace= " Http://www.zjh.com/SU ")]
[WebMethod]
public string GetName (string name)
{
Return "Hello" +name;
}  

Then run it and use IE to test successfully.

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

Java code:
<% @page pageencoding= "GBK" contenttype= "text/html; CHARSET=GBK "%>
<% @page import= "Org.apache.axis.client.Call"%>
<% @page import= "Org.apache.axis.client.Service"%>
<% @page import= "Javax.xml.namespace.QName"%>
Java code
<%
Request.setcharacterencoding ("GBK");
String name=request.getparameter ("name");
String endpoint= "Http://localhost/HelloClient/Service1.asmx";
Service service = new service ();
Call Call Service.createcall ();
Call.settargetendpointaddress (new Java.net.URL (endpoint));
Call.setoperationname (New QName ("Http://www.zjh.com/SU", "GetName"));
Call.addparameter ("name", org.apache.axis.encoding.xmltype.xsd_date,javax.xml.rpc.parametermode.in);
Call.setreturntype (Org.apache.axis.encoding.XMLType.XSD_STRING);
Call.setusesoapaction (TRUE);
Call.setsoapactionuri ("http://www.zjh.com/Rpc");
string res = (string) call.invoke (new Object[]{name});
Out.print (RES);
%>

This is more complex than the WS that Java calls Java to write. WS and Java clients have to set SOAPAction, otherwise there will be an exception to say System.Web.Services.Protocols.SoapException: The server failed to recognize the value of the HTTP header SOAPAction


V: summary
After two days of experimentation, it was finally technically clear that the interoperability of. NET with Java through Web services was possible. Of course, there are a few points to note:

1. When providing Web services, try to use the variable types supported in the XML Schema as a parameter. If you use a dataset of. NET this type, parsing for Java will be a disaster, of course, theoretically can be resolved. However, 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 this type of dataset, the system also needs to serialize it, which is a resource-intensive process. Using the string type will be much simpler.

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

Citation: http://www.cnblogs.com/lzhdim/archive/2008/12/21/1359308.html

Java and. NET WebServices call each other

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.