解决gsoap中文乱码的问题


解决方法一:

在main函数里初始化soap结构体后加入
    soap_set_mode(&soap,SOAP_C_UTFSTRING);

这样所有的C都是utf-8的格式,只要你的windows客户端支持UTF-8格式就没有了乱码问题

解决方法二:iconv库转码 

一般在ubuntu中都会预装iconv的库,使用也很简单,网上的教程很多..
下面的函数,在需要转码的地方调用即可
#include <iconv.h>
#ifdef ICONV_EN 
/*
   用户中文乱码问题,格式转换用!
ARG:
dest:目的格式
src:原格式
input:输入字符串
...*/

int conv_charset(const char *dest, const char *src, char *input, size_t ilen, char *output, size_t olen) {
        char **inbuf = &input;
        char **outbuf = &output;
        if(dest == NULL || src == NULL)
                return -1; 
        iconv_t conv = iconv_open(dest, src);
        if ( conv == (iconv_t) -1 )
                return -1; 
        memset(output, 0, olen);
        printf("dest=%s,src=%s,input=%s,output=%s,olen=%d
",dest,src,*inbuf,*outbuf,olen);
        if ( iconv(conv, inbuf, &ilen, outbuf, &olen) )
                return -1; 
        printf("output=%s
",output); //此处打印output为空,正常
        iconv_close(conv);
        return 0;
}
#endif


函数的使用,一般windows支持GBK和GB2312格式,现在的windows强制支持GB18030,转为这个格式很好的!
函数都是亲测可用的,给大家分享,一块进步...

int send_msg_xml(struct soap *soap,char *msg)
{
        struct Namespace *nsmap;
        nsmap = NULL;
        soap->encodingStyle = NULL;
        soap_set_omode(soap,SOAP_XML_TREE);
        soap->http_content = "text/xml";
        soap_set_namespaces(soap,nsmap);
        soap_begin_send(soap);
#ifdef ICONV_EN 
        char *buff/*[OUTPUT_LEN]={0}*/;
        //转码
        buff = (char *)malloc(OUTPUT_LEN);
        if(conv_charset("GBK","UTF-8",msg,(int)strlen(msg),buff,(int)OUTPUT_LEN) != 0)
        {   
#endif
                soap_serialize_string(soap, &msg);
                soap_put_string(soap, &msg, "RET_MSG", NULL);
#ifdef ICONV_EN     
        }else{
                soap_serialize_string(soap, &buff);
                soap_put_string(soap, &buff, "RET_MSG", NULL);
        }   
#endif
        soap_end_send(soap); // Clean up temporary data used by the serializer
        return SOAP_OK;


}


原文地址:https://www.cnblogs.com/jiangu66/p/3163003.html