Java call to WebService.

Source: Internet
Author: User
This is an example of using Java to call the C # version of the WebService interface:
C # Interface:

<span style= "FONT-SIZE:11PX;" >using system;using system.web;using system.web.services;using system.web.services.protocols;using System.Web.Services.Description; [WebService (Namespace = "http://www.tangs.com/")] [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]public class service:system.web.services.webservice{ Public Service () ... {//If you are using a design component, uncomment the following line//initializecomponent ();} [SoapRpcMethod (Action = "Http://www.tangs.com/Add", Requestnamespace = "Http://www.tangs.com/T", Responsenamespace = " Http://www.tangs.com/T ", use = soapbindinguse.literal)] [webmethod]public int Add (int a, int b) ... {return a + B;} [SoapRpcMethod (Action = "Http://www.tangs.com/Hello", Requestnamespace = "Http://www.tangs.com/T", Responsenamespace = "Http://www.tangs.com/T", use = soapbindinguse.literal)] [webmethod]public String HelloWorld () ... {return "Hello, world!";}} ...</span>

Java's call to add methods and HelloWorld methods in this Web service:

1, Parameter method: Add

<span style= "font-size:11px;" >public static void Addtest () {Try ... {Integer i = 1; Integer j = 2;//webservice URL String service_url = "Http://localhost:4079/ws/Service.asmx"; Service service = new service (); Call Call Service.createcall (); Call.settargetendpointaddress (New Java.net.URL (Service_url));//sets the method call.setoperationname to be called (The New QName ("http:// Www.tangs.com/T "," ADD ");//The method requires a parameter Call.addparameter (" A ", Org.apache.axis.encoding.XMLType.XSD_INT, Javax.xml.rpc.ParameterMode.IN); Call.addparameter ("B", Org.apache.axis.encoding.XMLType.XSD_INT, Javax.xml.rpc.ParameterMode.IN);// The return value type of the method Call.setreturntype (Org.apache.axis.encoding.XMLType.XSD_INT); Call.setusesoapaction (TRUE); Call.setsoapactionuri ("Http://www.tangs.com/Add");//Call the method Integer res = (integer) call.invoke (new object[] ... {i, J}); System.out.println ("Result:" + res.tostring ());} catch (Exception e) ... {System.err.println (e);} }...</span> 

Run, result returned: Result: 3

2. Non-parametric method: HelloWorld

<span style= "FONT-SIZE:11PX;" >public static void Hellotest () {Try ... {String endpoint = "Http://localhost:4079/ws/Service.asmx"; Service service = new service (); Call Call Service.createcall (); Call.settargetendpointaddress (new Java.net.URL (endpoint)); Call.setoperationname (New QName ("Http://www.tangs.com/T", "HelloWorld")); Call.setusesoapaction (TRUE); Call.setsoapactionuri ("Http://www.tangs.com/Hello"); string res = (string) call.invoke (new object[] ... {null}); System.out.println ("Result:" + res);} catch (Exception e) ... {System.err.println (e.tostring ());} }...</span>

As you can see, calling a Web service with no parameters is basically the same as having a parameter, but without a call, you do not need to call the Addparameter method and the Setreturntype method
Execute Query Query Results Report view: Hello, world!

Attachments in jar packages that are dependent on the Web service

  • 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.