Java code calls the Web service implemented by C #

Source: Internet
Author: User
Tags wsdl

1.WebService Project Structure

Simplemodel class:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacedonetws{ Public classSimplemodel { PublicSystem.Int32 ID {Set; Get; }         PublicSystem.String Str {Set; Get; }    }}

Donetws.asmx Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;namespacedonetws{/// <summary>    ///Summary description of Donetws/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]    //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService]     Public classDonetWS:System.Web.Services.WebService {//Test Pass[WebMethod] Public stringHelloWorld (stringname) {            return "Hello World,"+name; }        //test Get collection of objects[WebMethod] PublicList<simplemodel>getmodellist () {List<SimpleModel> Rsltdata =NewList<simplemodel>(); Simplemodel Model=NewSimplemodel (); Model.id=1; Model.str="str1";            Rsltdata.add (model); Model=NewSimplemodel (); Model.id=2; Model.str="str2";            Rsltdata.add (model); returnRsltdata; }    }}

Run up:

2. Using the Apache CXF framework for client invocation

  

In the Java Project Pom.xml, add:

<Dependency>    <groupId>Org.apache.cxf</groupId>    <Artifactid>Cxf-rt-frontend-jaxws</Artifactid>    <version>3.0.3</version></Dependency><Dependency>    <groupId>Org.apache.cxf</groupId>    <Artifactid>Cxf-rt-transports-http</Artifactid>    <version>3.0.3</version></Dependency>

Jars

Open cmd, under the Cxf Directory Bin folder

Run: wsdl2java-p org.tempuri-d src-client http://localhost:26715/DonetWS.asmx?wsdl

The following classes are generated:

  

Leave only these 4 classes in the Java project's Org.tempuri package

Java Project structure

Cxfclient Class Code

Importjava.util.List;ImportJava.util.ResourceBundle;Importorg.apache.cxf.endpoint.Client;Importorg.apache.cxf.endpoint.dynamic.DynamicClientFactory;ImportOrg.tempuri.ArrayOfSimpleModel;ImportOrg.tempuri.SimpleModel; Public classcxfclient { Public StaticResourceBundle r = resourcebundle.getbundle ("config");  Public Staticdynamicclientfactory DCF =dynamicclientfactory.newinstance ();  Public StaticClient client = dcf.createclient (r.getstring ("Wsdllocation")));  Public Static voidSynchelloworld () {Try{object[] replys= Client.invoke ("HelloWorld", "Darkdog");  for(Object reply:replys) {String rsltdata=(String) reply;            System.out.println (Rsltdata); }        } Catch(Exception e) {e.printstacktrace (); }    }     Public Static voidsyncgetmodellist () {Try{object[] replys= Client.invoke ("Getmodellist");  for(Object reply:replys) {Arrayofsimplemodel Arrayofsimplemodel=(Arrayofsimplemodel) reply; List<SimpleModel> Simplemodels =Arrayofsimplemodel.getsimplemodel ();  for(Simplemodel simplemodel:simplemodels) {System.out.println (Simplemodel.getid ()+ " " +simplemodel.getstr ()); }            }        } Catch(Exception e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {Synchelloworld ();    Syncgetmodellist (); }}

Config.properties:

Wsdllocation=http\://localhost\:26715/donetws.asmx?wsdl

Complete!

Java code calls the Web service implemented by C #

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