需要装Soap Toolkit,以下代码在VC++6.0中测试成功。
在stdafx.h加上
//WEB_SERVICE
#include <stdio.h>
#import "msxml4.dll"
using namespace MSXML2;
#import "C:/Program Files/Common Files/MSSoap/Binaries/mssoap30.dll" /
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", /
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;
//END
SoapTestDlg.h里面
CString BeginSoap(CString,CString,CString);
ISoapConnectorPtr SoapConnector;
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
SoapTestDlg.cpp里面
void CSoapTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str,strWord;
GetDlgItem(IDC_EDIT1)->GetWindowText(strWord);
str=BeginSoap("EnglishTOChinese",strWord,"http://www.webservicex.net/translateservice.asmx");
AfxMessageBox(str);
}
CString CSoapTestDlg::BeginSoap(CString UserName,CString Password,CString WebUrl)
{
HRESULT hr;
try
{
//创建SoapConnector类的对象
SoapConnector.CreateInstance(__uuidof(HttpConnector30));
//指定Web服务的地址
SoapConnector->Property ["EndPointURL"] =(LPSTR)(LPCTSTR)WebUrl;
//与Web服务连接
hr=SoapConnector->Connect();
if(FAILED(hr)) return "";
//指定Web服务完成的操作
SoapConnector->Property ["SoapAction"] = _T("http://www.webservicex.net/Translate");
//准备发送消息给Web服务
SoapConnector->BeginMessage();
// 创建SoapSerializer对象
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)SoapConnector->InputStream));
// 创建SOAP消息
Serializer->StartEnvelope("soap","","");
Serializer->StartBody("");
Serializer->StartElement("Translate","http://www.webservicex.net","","soap");
Serializer->StartElement("LanguageMode","","","soap");
Serializer->WriteString((_bstr_t)(LPCTSTR)UserName);
Serializer->EndElement();
Serializer->StartElement("Text","","","soap");
Serializer->WriteString((_bstr_t)(LPCTSTR)Password);
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
//消息真正地发给Web服务
hr=SoapConnector->EndMessage();
if(FAILED(hr)) return "";
// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));
// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)SoapConnector->OutputStream), _T(""));
return CString((const char *)Reader->RpcResult->text);
}
catch (_com_error e)
{
return (CString)(char*)e.Description();
}
}