在VC++6.0中调用Web Services的方法

 需要装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();
 }
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值