C + + and Java communication via WebService (above)

Source: Internet
Author: User
Tags soap wsdl

First, preface

This article describes the WebService interface for SOAP schemas that are published through a C + + client access to the Java service side. The sample code in the document is copied out to run, all the code is local test OK, this article not only solves the problem of interface call, but also solves the problem of Chinese garbled.

Second, the Environment preparation

1, CXF components: Java-side for publishing WebService services open source components, internal jetty Web container.
2, gSOAP components: C + + end user access to WebService services components. The gsoap-2.8 is used. These two components can be downloaded directly from Google, the official website.
3, Eclipse:java development IDE.
4, VS 2010:c++ development IDE.

Java service-side development STEP1 Java Service Interface development

1. Create a new Java project and import the jar package under the cxf Lib directory.
2, define the WebService interface.

import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublicinterface WSWebServiceIF {    @WebMethod    publichelloWebService();    @WebMethod    publichelloWebService2(String param);}

3. Implement WebService interface

ImportJava.nio.charset.Charset; Public  class wswebservice implements Wswebserviceif{    @Override     PublicStringHellowebservice()     {return "Hello WS webservice!"; }@Override     PublicStringHelloWebService2(String param) {System.out.println ("received input parameter:"+ param); String str ="1212312abns return value with Chinese. ";return NewString (Str.getbytes (Charset.forname ("UTF-8"))); }}
STEP2 Publishing WebService Services with CXF
ImportOrg.apache.cxf.jaxrs.JAXRSServerFactoryBean;ImportOrg.apache.cxf.jaxws.JaxWsServerFactoryBean; Public  class StartUp {     Public Static void Main(string[] args) {Jaxwsserverfactorybean Jwfbean =NewJaxwsserverfactorybean ();//Publish the service to Port 10009 on this machine, the service name is WS. Jwfbean.setaddress ("Http://localhost:10009/WS");        Jwfbean.setserviceclass (Wswebserviceif.class); Jwfbean.setservicebean (NewWswebservice ()); Jwfbean.create ();//blocking threads, waiting for external message requests.          while(true)        {Try{Thread.Sleep ( the); }Catch(Interruptedexception e)            {E.printstacktrace (); }        }    }}

With CFX, developing the WebService service in the Java language is very simple. Environment configuration is simple, a few lines of code will be done.
Note: localhost is set correctly in the host file if it is set to another invalid IP. Will cause service publishing to fail.
The service's interface file (WSDL) can be viewed in the browser after a successful publication.

Iv. C + + client development STEP1 use Gsoap-2.8\gsoap\bin\win32\wsdl2h.exe to generate a client header file.

Execute the command line and generate the Hellowebservice.h header file in the same directory as the Wsdl2h.exe. Generates a C + + version without using the STL header file. The Wsdl2h.exe command also has many parameters to choose from, and wsdl2h.exe–h displays help information.

STEP2 uses gsoap-2.8\gsoap\bin\win32\ soapcpp2.exe to generate a pile file. With these pile functions, you can access the WebService service on a remote server in C + + as you would call a local function.

Soapcpp2.exe also has many parameters to choose from, enter Soapcpp2.exe–h to see Help. The following files are generated after executing the above command (copy the files in the red box to the VS2010 project):

Stdsoap.h and Stdsoap.cpp files are copied from the Gsoap-2.8\gsoap directory.

STEP3 developing client calling code
#include "stdafx.h"#include "soapH.h"//UTF-8 characters are summoned as wide characters for display in the consolewchar_t* Mulitybytetowidechar (UINT CodePage,Char*Str) {DWORD Dwnum = MultiByteToWideChar (CodePage,0,Str, -1,0,0); wchar_t *pwtext =NewWchar_t[dwnum]; MultiByteToWideChar (CodePage,0,Str, -1, Pwtext, Dwnum);returnPwtext;}//Convert wide characters to UTF-8 mode. Used to pass parameters. Char* Widechartomulitybyte (UINT CodePage, wchar_t *Str){intLen = WideCharToMultiByte (CodePage,0,Str, -1,0,0,0,0);Char* Output =New Char[Len +2]; WideCharToMultiByte (CodePage,0,Str, -1, output, Len +1,0,0);returnOutput;}int_tmain (intARGC, _tchar* argv[]) {struct SOAP soapobject;    Soap_init (&soapobject); Soap_set_mode (&soapobject, soap_c_utfstring);//test code, path hard-coded. ConstChar*Server="HTTP://LOCALHOST:10009/WS/WSWEBSERVICEIF?WSDL";//Call an interface with no input parametersNs1__hellowebserviceresponse RSP;    Ns1__hellowebservice ns1; Soap_call___ns1__hellowebservice (&soapobject,Server,"", &NS1, RSP);//Call an interface with an input parameter and a return value. The input parameter in the sample code is a string, the return value is also    //A string. You need to pass more information to add more arguments, or you can pass a JSON string and wrap the required content inside.     //recommend the latter approach. Ns1__hellowebservice2 NS11; ns11.arg0 = Widechartomulitybyte (Cp_utf8, L"12341asasas with Chinese");    Ns1__hellowebservice2response rsp2; Soap_call___ns1__hellowebservice2 (&soapobject,Server,"", &NS11, RSP2);    wchar_t* Wcreturn = Mulitybytetowidechar (Cp_utf8, rsp2.return_); printf"received return value:%s\n", Wcreturn);    Soap_end (&soapobject); Soap_done (&soapobject);return 0;}
STEP3 Debug Verification

Start the Java server with Eclipse, and then start the client in VS2010, one-step debugging. View interface calls and arguments, return value information.

C + + and Java communication via WebService (above)

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.