1. References
1. How http://www.cnblogs.com/xuqifa100/archive/2007/12/13/993926.html uses. Net to publish Web Service
2. Cross-language WebService Problems
3. Why is it so difficult for Java to call DOTNET WebService?
4. Example of Java calling the. NET service
5. Use axis to call the. NET Server
Ii. Overview
I wrote a blog about eclipse + WebService in the Java environment. Considering the cross-system, cross-language, and cross-network features of WebService, an instance is written below to test its cross-language features.
First, use Asp.net to develop a WebService, and then create a client in Java to call this service.
Iii. Instances
(1) Open Visual Studio 2010 and create a project, as shown in:
Shows the results of the new project:
(2) Add a service method to service1.asmx. CS,CodeAs follows:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. services; </P> <p> namespace aspwebservice1 <br/> {<br/> /// <summary> <br/> // service1 summary <br/>/ // </Summary> <br/> [WebService (namespace = "http://erplab.sjtu.edu/")] <br/> [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] <br/> [system. componentmodel. toolboxite M (false)] <br/> // to allow ASP. Net ajax to call this web service from a script, uncomment the downstream service. <Br/> // [system. web. script. services. scriptservice] <br/> public class service1: system. web. services. webService <br/>{</P> <p> [webmethod] <br/> Public String helloworld () <br/>{< br/> return "Hello World "; <br/>}</P> <p> [webmethod] <br/> Public String sayhellotopersonnew (string name) <br/>{< br/> If (name = NULL) <br/>{< br/> name = "nobody "; <br/>}< br/> return "hello," + name; <br/>}</P> <P> [webmethod] <br/> Public double count (double number, double price, double discount) <br/>{< br/> return number * price * discount; <br/>}</P> <p> [webmethod] <br/> public float getfloat (float X) <br/>{< br/> return X; <br/>}</P> <p> // addition <br/> [webmethod] <br/> public float plus (float X, float y) <br/>{< br/> return X + Y; <br/>}</P> <p> // subtraction <br/> [webmethod] <br/> public float minus (floa T x, float y) <br/>{< br/> return x-y; <br/>}</P> <p> // multiplication <br/> [webmethod] <br/> public float multiply (float X, float y) <br/>{< br/> return x * Y; <br/>}</P> <p> // Division <br/> [webmethod] <br/> public float divide (float X, float y) <br/>{< br/> If (y! = 0) <br/>{< br/> return x/y; <br/>}< br/> else <br/> return-1; <br/>}</P> <p >}< br/>}(3) Publish the service. Press Ctrl + F5 to run the project. The Service Homepage is http: // localhost: 5329/service1.asmx, as shown in:
Is the service method defined in the service1.asmx. CS file. Click "service description" to view the WSDL file of WebService.
(4) Compile a Java client to test WebService and JavaProgramAs follows:
Package Edu. SJTU. erplab. aspwebservice; </P> <p> Import javax. XML. namespace. QNAME; <br/> Import javax. XML. RPC. parametermode; <br/> Import Org. apache. axis. client. call; <br/> Import Org. apache. axis. client. service; <br/> Import Org. apache. axis. encoding. xmltype; </P> <p> public class aspwebservicetestclient1 {</P> <p> Public static void main (string [] ARGs) throws exception {<br/> // definition method <br/> string method = "Hello World "; <br/> string methodplus =" plus "; <br/> string methodminus =" minus "; <br/> string methodmultiply =" Multiply "; <br/> string methoddivide = "divide"; <br/> string methodsayto = "sayhellotopersonnew "; <br/> // define the service <br/> service = new service (); </P> <p> // Test 1: Call the helloworld method, method does not have parameters <br/> call = (CALL) service. createcall (); <br/> call. settargetendpointaddress (New java.net. URL (<br/> "http :/ /Localhost: 5329/service1.asmx "); <br/> call. setusesoapaction (true); <br/> // The first method to set the return value type to string <br/> call. setreturntype (xmltype. soap_string); <br/> call. setoperationname (New QNAME ("http://erplab.sjtu.edu/", method); <br/> call. setsoapactionuri ("http://erplab.sjtu.edu/HelloWorld"); <br/> string retval1 = (string) Call. invoke (new object [] {}); <br/> system. out. println (retval1); </P> <p> // Test 2: Call sayhell The otopersonnew method has a parameter: Name. Sayhellotopersonnew (string name) <br/> call call2 = (CALL) service. createcall (); <br/> call2.settargetendpointaddress (New java.net. URL (<br/> "http: // localhost: 5329/service1.asmx"); <br/> call2.setusesoapaction (true ); <br/> call2.setreturntype (New QNAME ("http://www.w3.org/2001/XMLSchema", <br/> "string ")); <br/> // method for setting the return value type to string <br/> call2.setoperationname (New QNAME ("http://erplab.sjtu.edu/", meth Odsayto); <br/> call2.setsoapactionuri ("http://erplab.sjtu.edu/sayHelloToPersonNew"); <br/> call2.addparameter (New QNAME ("http://erplab.sjtu.edu/", "name "), // The name corresponds to the parameter name <br/> xmltype. xsd_string, parametermode. in); <br/> string retval2 = (string) call2 <br/>. invoke (new object [] {"ASP WebService"}); <br/> system. out. println (retval2); </P> <p> // Test 3: Call the sgetfloat method. The method has a parameter: X, float type. <br/> call call3 = (CALL) service. createcall (); <br/> call3.settargetendpointaddress (New java.net. URL (<br/> "http: // localhost: 5329/service1.asmx"); <br/> call3.setusesoapaction (true); <br/> call3.setencodingstyle (null ); // required. If not, an error is returned. The most critical statement. Determine to generate the soap namespace in xmlns <br/> // The first method to set the return value type to float <br/> call3.setreturntype (Org. apache. axis. encoding. xmltype. xsd_float); <br/> call3.setoperationname (New QNAME ("http://erplab.sjtu.edu/", "getfloat"); <br/> call3.setsoapactionuri ("http://erplab.sjtu.edu/getFloat "); <br/> call3.addparameter (New QNAME ("http://erplab.sjtu.edu/", "x"), // The Name Of The X parameter <br/> xmltype. xsd_float, parametermode. inout); <br/> float Retval3 = (float) call3.invoke (new object [] {123}); <br/> system. out. println (retval3); </P> <p> // Test 4: Call the plus method. The method has two parameters: X and Y. Plus (float X, float y) <br/> call call4 = (CALL) service. createcall (); <br/> call4.settargetendpointaddress (New java.net. URL (<br/> "http: // localhost: 5329/service1.asmx"); <br/> call4.setusesoapaction (true); <br/> call4.setencodingstyle (null ); <br/> // method for setting the return value type to float <br/> call4.setreturntype (New QNAME ("http://www.w3.org/2001/XMLSchema", <br/> "float ")); <br/> call4.setoperationname (New QNAME ("http://erplab.sjtu.edu/", methodplus); // addition <br/> call4.setsoapactionuri ("http://erplab.sjtu.edu/plus "); <br/> call4.addparameter (New QNAME ("http://erplab.sjtu.edu/", "x"), // parameter x <br/> Org. apache. axis. encoding. xmltype. xsd_float, parametermode. in); <br/> call4.addparameter (New QNAME ("http://erplab.sjtu.edu/", "Y"), // Parameter Y <br/> xmltype. xsd_float, parametermode. in); <br/> float retval4 = (float) call4.invoke (new object [] {5, 6}); <br/> system. out. println (retval4); <br/>}< br/>Running result:
-Unable to find required classes (javax. activation. datahandler and javax. mail. internet. mimemultipart ). attachment support is disabled. <br/> Hello World <br/> hello, ASP WebService <br/> 123.0 <br/> 11.0 <br/>
Note:
(A) If the parameter is of the string type, you do not need to set the call parameter.Call3.setencodingstyle (null );However, if the input parameter is of the float type, this statement must be added.
(B) There are two methods to set the return value type:
One is
Call. setreturntype (xmltype. soap_string );The other is
Call2.setreturntype (New QNAME ("http://www.w3.org/2001/XMLSchema", "string "));The two methods are equivalent.