65,186
社区成员




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;
}
}
}
#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>
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;
}
编码转换,依旧不行