woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

SOAP中发中文的VC示例

在Pwin97/Win2000 Professional下运行通过。
#include <windows.h>
#include <easysoap/SOAP.h>
USING_EASYSOAP_NAMESPACE
//
//  The namespace for the methods we're calling.  This
//  is the same namespace defined in calchandler.h.
int main(int argc, const char *argv[])
{
    int iRet = 0;
    unsigned short* strWideChar = NULL;
    char* strANSIReturn = NULL;
    try
    {
        const char * strUrl = "http://169.254.45.130/WS/Service1.asmx?op=HelloWorld";
        const char * strNameSpace = "http://tempuri.org/";
        const char * strSOAPAction = "http://tempuri.org/HelloWorld";
        const char * strMethodName = "HelloWorld";
        SOAPString url(strUrl);
        SOAPUrl endpoint(url);
        SOAPProxy proxy(endpoint);
        SOAPMethod method(strMethodName, strNameSpace, strSOAPAction);
        SOAPString inputValue;
        char str[5] = "中文";
        int iLenWideChar = MultiByteToWideChar(0, 0, const_cast<char *>(str), sizeof(str), NULL, 0);
        strWideChar = new unsigned short[iLenWideChar];
        if(NULL == strWideChar)
        {
            goto Exit;
        }
        int iLenRet = MultiByteToWideChar(0, 0, const_cast<char *>(str), sizeof(str), strWideChar, iLenWideChar);
        inputValue.Append(strWideChar);
        method.AddParameter("a") << 1;
        method.AddParameter("b") << inputValue;
        const SOAPResponse& response = proxy.Execute(method);
        SOAPString strRet = response.GetReturnValue().GetString();
        //get return value (UTF-8 encoding)
        char * strReturn = strRet.Str();
        unsigned int iLenReturn = sp_strlen(strReturn);
        SOAPArray<unsigned short> UTF8Return(iLenReturn);
        char * iterator = strReturn;
        //convert UTF-8 to WideChar
        for(unsigned int i=0; iterator <= (strReturn+iLenReturn) && i<iLenReturn; ++i)
        {
            sp_UTF8_UCS(iterator,UTF8Return[i]);
        }
        //UTF-8 to UTF16
        int iLenANSIReturn = WideCharToMultiByte(0, 0, UTF8Return, -1, NULL, 0, NULL, NULL);
        strANSIReturn = new char[iLenANSIReturn];
        if(NULL == strANSIReturn)
        {
            goto Exit;
        }
        WideCharToMultiByte(0, 0, UTF8Return, -1, strANSIReturn, iLenANSIReturn, NULL, NULL);
        printf("ANSI:%s\n", strANSIReturn);
        getchar();
    }
    catch (SOAPException& ex)
    {
        printf("Caught SOAP exception: %s\n", ex.What().Str());
        iRet = 1;
    }
Exit:
    if(NULL != strWideChar)
    {
        delete [] strWideChar;
        strWideChar = NULL;
    }
    if(NULL != strANSIReturn)
    {
        delete [] strANSIReturn;
        strANSIReturn = NULL;
    }
    return iRet;
}

posted on 2008-05-27 18:17 肥仔 阅读(575) 评论(0)  编辑 收藏 引用 所属分类: 网络编程


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理