C++ 调用WebService(C#)乱码问题

「已注销」 2014-09-11 03:38:04
直接如主题吧:
WebService服务端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace WebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public string Hello(string input)
{
return "Hello " + input;
}
}
}

客户端(VC6.0):

#include <stdio.h>
#include "windows.h"
#include "Atlbase.h"
#import "msxml4.dll"
using namespace MSXML2;
#include <string>

using namespace std;

char* WSDLURLTEST = "http://localhost/WebService/Service1.asmx?WSDL";

void SayHello(string strContent,string& strResp);

int main()
{
SayHello("你好!",strRsp);
getchar();
return 0;
}

void SayHello(string strContent,string& strResp)
{
//CString cstrCon = strContent.c_str();
//CString str;
//zhToUtf8(cstrCon, str);
//strContent = str.GetBuffer(str.GetLength());
CoInitialize(NULL);
try
{
IXMLHTTPRequestPtr xmlRequest;//定义http请求对象
xmlRequest.CreateInstance(__uuidof(XMLHTTP));//创建实列
CComVariant vFalse(FALSE);
CComVariant vNull(NULL);
xmlRequest->open("POST",_bstr_t(WSDLURLTEST),vFalse,vNull,vNull);//打开WEBServeice方法:加?wsdl
xmlRequest->setRequestHeader(_bstr_t(_T("Content-Type")), _bstr_t(_T("text/xml")));

string sb;
sb.append("<?xml version='1.0' encoding='utf-8'?>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");

sb.append("<soap:Body>");
sb.append("<Hello xmlns=\"http://tempuri.org/\">");
sb.append("<input>");sb.append(strContent);sb.append("</input>");
sb.append("</Hello>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>");

xmlRequest->send(_variant_t(sb.c_str()));//发送数据

BSTR bstrbody;
xmlRequest->get_responseText(&bstrbody);//得到返回数据
_bstr_t bstrtbody(bstrbody);

printf("%s\n",(LPCTSTR)bstrtbody);

MSXML2::IXMLDOMDocument2Ptr m_xmldoc;
m_xmldoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
m_xmldoc->loadXML(bstrbody);

MSXML2::IXMLDOMNodePtr node = m_xmldoc->documentElement->firstChild;
strResp=(string)m_xmldoc->documentElement->text;
}
catch(_com_error &e)
{
printf("SayHello Error: %s",(char*)e.Description());
}
}


打印结果:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.
xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloResponse xmlns="
http://tempuri.org/"><HelloResult>Hello ?????????</HelloResult></HelloResponse><
/soap:Body></soap:Envelope>

返回结果中的中文都是乱码。
请问各位这个问题怎么解决?
...全文
274 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2014-09-29
  • 打赏
  • 举报
回复
gsoap soap_set_mode(&proxy,SOAP_C_UTFSTRING); + 编码转换
「已注销」 2014-09-12
  • 打赏
  • 举报
回复
引用 1 楼 xian_wwq 的回复:
乱码是因为编码格式不对, 好像c#的WebService默认用的是Unicode
经过如下:
char* MBCS2Utf8(char* szMBCS, ULONG* _out_length)
{
    if (szMBCS == NULL || _out_length == NULL)
        return NULL;
    // 方法:先转换成CP_ACP再转换成CP_UTF8
    int nLength = MultiByteToWideChar(CP_ACP, 0, szMBCS, -1, NULL, NULL);   // 获取缓冲区长度,再分配内存
    WCHAR *tch = new WCHAR[nLength];
     
    nLength = MultiByteToWideChar(CP_ACP, 0, szMBCS, -1, tch, nLength);     // 将MBCS转换成Unicode
 
    int nUTF8len = WideCharToMultiByte(CP_UTF8, 0, tch, -1, 0, 0, 0, 0);   // 获取UTF-8编码长度
    char *utf8_string = new char[nUTF8len];
    WideCharToMultiByte(CP_UTF8, 0, tch, -1, utf8_string, nUTF8len, 0, 0); //转换成UTF-8编码
    *_out_length = nUTF8len;
 
    delete tch;
    return utf8_string;
}
编码转换,依旧不行
xian_wwq 2014-09-12
  • 打赏
  • 举报
回复
乱码是因为编码格式不对, 好像c#的WebService默认用的是Unicode

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧