C++ calls webService service to solve the problems of Chinese garbled code and file uploading


I wrote an article about c++ publishing webService service before. Unfortunately, I read very little, but it did help a person in need. It is a little gratifying. Now I will write another article about c++ calling webService service, including Chinese transfer and file upload.

The project needs this thing. I will write a code example. The specific project code will not be released. There are many pitfalls encountered. Fortunately, they have been solved one by one. It is necessary to surf the Internet scientifically, which can solve many problems. What's abominable is that some people on the network actually use the code samples uploaded from the file to get 34 points. It's really abominable. There is no spirit of open source at all. Here we should despise such people.

1, gsoap2.8

The gsoap I downloaded here is the latest 2.8. If we want to upload Chinese, we will encounter the problem of garbled code. Here we need to solve the problem of garbled code, otherwise the garbled code will be displayed when c++ is uploaded to the webService interface. The interface here is written in java. For a simple explanation, I will integrate the Chinese parameter transfer with the file, so I won't demonstrate it one by one.

Here is my interface code. I use springboot to publish the webService interface with the cxf framework. This is not the point.

java interface code example:

public String fileCall(DataHandler file, String params) {

First, we need to know the wsdl address of the interface. We need to use gsoap to generate relevant proxy classes based on the wsdl address.

Key: create an API in the following directory Bat (the name can be chosen at will. It is recommended to use the project name)

Reason: this involves the calling of Chinese strings, so it needs to be handled in this way.

Here I wrote it in win64 bit code. If there is a need, it can also be generated under win32. The steps are the same.

Document content:

xsd__string = | std::wstring | wchar_t*

Open the cmd command window under the current directory, shift+ left mouse button

Execute the following command:

Generally speaking, they will succeed as long as they strictly follow my requirements

//This is the first command. The api H header file

.\wsdl2h.exe -o .\api.h http://localhost:8081/api/services/api?wsdl -t api.bat     

//This is the second command. After execution, the following file will appear

.\soapcpp2.exe -C .\api.h -I .\api\ -x

2, Writing test call code

Now the most critical point has come. Here we have to criticize some people again and again. The code of c++ calling webService even needs to download and collect points! It's really hateful.

Moreover, a certain domestic search is not awesome, so I directly used the scientific Internet, and then used my poor English to finally get a relatively perfect c++ file upload document. I can't wait to publish it after making some changes.

OK, no more nonsense, just go ahead!

Import related header files. Note here that you also need to import two files in the gsoap folder. Do not import too many import files!

Imported files:

Don't pretend to use clion wisely here. Use VS2010 or other versions honestly. For the same code, vs can. Clion reports an error, saying that it can't find the relevant definition. I won't roast here.

To write test code, we write a test CPP, code as follows

I uploaded a local file to the webService service, and then passed some parameter information

xsd__base64Binary is the key class for generating code

#include <iostream>
#include <stdio.h>
#include "soapH.h"
#include "IApiWebServiceSoapBinding.nsmap"
#include <fstream>
using namespace std;
using std::ifstream;

int main(){
    //SOAP client
    struct soap m_oSoap;

	//Test file call object
	class ns1__fileCall fileCallObject;

	//Test file return object
	class ns1__fileCallResponse fileCallResponse;

    //Here, we go to fileCallObject and find that we need to use this class
	class xsd__base64Binary remoteFile;

	//Declare a memory
	unsigned char *buffer;
	int fileLen;
	FILE *file;
    //What does this rb mean? I don't know
	file = fopen("D:\\test\\Test file upload text document.txt","rb");
		fprintf(stderr, "Unable to open file %s", "Test file upload text document.txt");
        return 0;
    fseek(file, 0, SEEK_END);
    fseek(file, 0, SEEK_SET);

    buffer=(unsigned char *)soap_malloc(&m_oSoap,fileLen+1);
    if (!buffer)
        fprintf(stderr, "Memory error! :( ");
        return 0;

    fread(buffer, fileLen, 1, file);

    //soap initialization
    //Call function parameter assignment

	fileCallObject.file = &remoteFile;
	fileCallObject.params=L"Test Chinese information";

    //Send a webService request
	int nResult = soap_call___ns1__fileCall(&m_oSoap,NULL,NULL,&fileCallObject,fileCallResponse);

    if (SOAP_OK == nResult) {
        //Output return result
        wchar_t *strResult = fileCallResponse.return_;
    //Close soap

    return 0;


ctrl+F5. Use VS to compile. After the compilation is successful, double-click to run

I set a breakpoint on the local interface, and I can clearly see the len length and buf information.

Chinese parameters. You can see that my parameter is prams = Test Chinese information

At this point, it will be done! Let's despise those who need points to download!

Tags: C++

Posted by dh526 on Tue, 31 May 2022 06:56:19 +0530