C + + and Java communication via WebService (bottom)

Source: Internet
Author: User
Tags soap wsdl

First, preface

This article describes how to access the WebService interface for SOAP schemas published by the C + + server through a Java client. 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.

Third, C + + server development Step1 Define WebService interface file HelloWebService.h
//gsoap ns service name: wscplus//gsoap ns service style: rpc//gsoap ns service namespace: http://localhost:10010/wscplus.wsdl//gsoap ns service location: http://localhost:10010//gsoap ns schema namespace: urn:wscplusint ns__hellowebservice(charchar** result);

Defining interfaces requires attention:
1, the interface return value is the int type.
2, the interface name definition format is ns__xxx.
3. The input parameter is a string pointer, and the output parameter is a pointer to the pointer. Again, more content can be passed through the JSON string.
4, the function head comments according to the sample definition. Define the service IP, port, and WSDL file name in the comments. If the function header is not defined in the specified format, the WSDL file will not be generated when using the Soapcpp2.exe conversion.

STEP2 uses gsoap-2.8\gsoap\bin\win32\ soapcpp2.exe to generate the service-side code.

The following files were generated:

STEP3 copy the head. H.cpp\nsmap to the VS2010 project, compile.

Compile however, comment out the # include "SoapC.cpp", #include "soapServer.cpp" in SoapServerLib.cpp.

STEP4 Implementing the WebService interface

Implementing the WebService Interface

int ns__hellowebservice(structcharchar** result){    wchar_t* param1 = MulityByteToWideChar(CP_UTF8, param);    printf("接收到Java客户端传过来的参数-param: %s\n", WideCharToMulityByte(CP_ACP, param1));    *result = WideCharToMulityByte(CP_UTF8, L"abKJLcd123e12一大堆中文输出参数");    return SOAP_OK;}

The first parameter of an interface implementation function is a struct soap* soapobject. The following parameters are consistent with the interfaces defined in HelloWebService.h. In order to ensure that the Chinese transmission is not garbled, the received parameters and return values are encoded conversion.

STEP5 implements the Http_get function, which returns the WSDL file information

Write a Get response request in order to enter a URL in the browser to see the WSDL file.
It is also convenient to invoke the defined interface directly in the WebService interface debugging tool such as SOAPUI.

intHttp_get (structsoap* soapobject) {file*fd =NULL;//WSCPLUS.WSDL is generated when the Soapcpp2.exe command is executed. Copy him to the current directory. FD = fopen ("WSCPLUS.WSDL","RB");//open WSDL file to copy    if(!FD) {return 404;//return HTTP not found error} soapobject->http_content ="Text/xml";//http Header with text/xml contentSoap_response (Soapobject, soap_file); for(;;) {size_t R = fread (Soapobject->tmpbuf,1,sizeof(SOAPOBJECT->TMPBUF), FD);if(!R) { Break; }if(Soap_send_raw (Soapobject, Soapobject->tmpbuf, R)) { Break;//cannot Send, but little we can does about that}} fclose (FD); Soap_end_send (Soapobject);returnSOAP_OK;}
STEP6 Release WebService Service
int_tmain (intARGC, _tchar* argv[]) {structSoap Soapobject;    Soap_init (&soapobject);    Soap_set_mode (&soapobject, soap_c_utfstring); Soapobject. Fget= Http_get; Soap_set_namespaces (&soapobject, namespaces);intret = Soap_bind (&soapobject,NULL,10010, -);if(Ret <0)    {return-1; } while(true)    {//blocking threads, waiting for external requestsret = soap_accept (&soapobject);if(Ret <0)        {return-1;        } soap_serve (&soapobject);    Soap_end (&soapobject); }return 0;}
Iv. Java Client Development STEP1 build a Java project and import the jar package in the cxf Lib directory.

I can't help but say that Java's Open source component is too good to use, fool-style development. C + + 's gSOAP is too much trouble.

STEP2 develops the client code and invokes the WebService interface.
Import Java. NiO. CharSet. Charset;Import Java. RMI. RemoteException; Import Javax. XML. Namespace. QName; Import Javax. XML. RPC. Parametermode;Import Javax. XML. RPC. Serviceexception;import org. Apache. Axis. Client. Pager; import org. Apache. Axis. Client. Service;import org. Apache. Axis. Encoding. XMLType;public class Startupclient {public static void main (string[] args) {try {String end Point ="HTTP://LOCALHOST:10010/WSCPLUS?WSDL"; Service service = new Service ();             Pager Pager= (Pager) Service. CreateCall();             Pager. Settargetendpointaddress(endpoint); Set the calling interface, specify input parameters, output parametersPager. Setoperationname(New QName ("Urn:wscplus","Hellowebservice"));             Pager. Addparameter("param", XMLType. XSD_string, Parametermode. Inch);            Pager. Addparameter("Result", XMLType. XSD_string, Parametermode. out);            Pager. Setreturntype(XMLType. XSD_string);String str ="237anastasiaabg293729 a lot of Chinese input parameters";String strparam = new String (str. GetBytes(Charset. forname("UTF-8")));String result = (string)Pager. Invoke(New Object[]{strparam});System. out. println("Get the C + + service-side return value-result:"+ result); } catch (Serviceexception e) {E. Printstacktrace(); } catch (RemoteException e) {E. Printstacktrace(); }    }}
Five, debugging verification

Start the server, start the client step in eclipse, can look at the output parameters, return value information.

C + + and Java communication via WebService (bottom)

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.