VC++6.0调用Web Services(可以是c#,java,vc++.net等提供的服务都可以)!

本文主要讲述VC++6.0调用Web Services的方法,其中web services可以是c#,java,vc++.net等提供的服务都可以调用!主要的大致过程如下:

     需要装Soap Toolkit3.0,电脑上一般自带Soap Toolkit1.0,以下代码在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()
{
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();
 }
}

//////////////////////////////////////////////////////////////////////OVER///////////////////////////

当然用Soap Toolkit1.0调用也可以,大致代码跟上面的一样,需要改动的有:

1.包含库文件代码改为下面

#import "msxml4.dll"
using namespace MSXML2;

#import "C:/Program Files/Common Files/MSSoap/Binaries/MSSOAP1.dll" /
exclude("IStream", "ISequentialStream", "_LARGE_INTEGER", /
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")

using namespace MSSOAPLib;

2. 函数代码上包含30数字的全将30去掉,如

SoapConnector.CreateInstance(__uuidof(HttpConnector));

Serializer.CreateInstance(__uuidof(SoapSerializer));
Reader.CreateInstance(__uuidof(SoapReader));

3.将

Serializer->StartElement("Text","","","soap");
  Serializer->WriteString((_bstr_t)(LPCTSTR)Password);
  Serializer->EndElement();
  
  Serializer->EndElement();
  
  Serializer->EndBody();
  Serializer->EndEnvelope();  
改为:(第一个单词小写)

Serializer->writeString("laghari78");
Serializer->endElement();
Serializer->endElement();
Serializer->endBody();
Serializer->endEnvelope();

/////////////////////////////////////////OK/////////////////////////////////////////////////

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭