VC 通过Soap访问WebService

http://blog.csdn.net/manytao/article/details/6288599

 在VC中,我们选择通过Soap规则来访问WebService,划分为以下步骤

 

1.所谓工欲善其事,必先利其器,既然选择Soap,我们首先要去微软官方下载soap sdk3.0安装包,地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C943C0DD-CEEC-4088-9753-86F052EC8450&displaylang=en

 

2.安装完成之后,打开C:/Program Files/Common Files/MSSoap/Binaries 路径,win7系统这里请注意,在win7系统下,默认只有C:/Program Files/Common Files/ 路径,需要自行添加,以及命令行中注册dll.具体可以参阅此文章http://blog.1wanweb.com/post/vc2b2b60-mfc-soapsdke5bc80e58f91websesrvicee68993e58c85.aspx

 

3.环境搭建完成之后,我们可以简单的通过一个MFC程序来实现一个通过Soap协议访问WebService的功能.

 

1.) 具体需求可划分为有一个edit,一个button按钮.具体Soap规范大家去google,一搜一大片,好好理解一下具体什么是soap协议,理解完成后可以继续看以下代码.

 

[cpp]  view plain copy
  1. void SoapTestDlg::OnOutput()   
  2. {  
  3.     CoInitialize(NULL);  
  4.     ISoapSerializerPtr Serializer;  
  5.     ISoapReaderPtr Reader;  
  6.     ISoapConnectorPtr Connector;  
  7.       
  8.     HRESULT hr=Connector.CreateInstance(__uuidof(HttpConnector30)); //创建连接实例  
  9.     if(!SUCCEEDED(hr))  
  10.     {    
  11.             AfxMessageBox("error");  
  12.         return;  
  13.     }  
  14.     //服务所在地址和端口;  
  15.     Connector->Property["EndPointURL"] = "http://localhost:1036/WebSite3/serviceForVC.asmx";  
  16.     Connector->Connect();  
  17.       
  18.     Connector->Property["SoapAction"] = "http://tempuri.org/GetExpertData"; // or http://61.156.8.197:8082/ExpertService.asmx/GetExpertData or http://61.156.8.197:8082/GetExpertData  
  19.     Connector->BeginMessage();  
  20.     Serializer.CreateInstance(__uuidof(SoapSerializer30));  
  21.     Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));  
  22.       
  23.       
  24.     Serializer->StartEnvelope("soap","http://schemas.xmlsoap.org/soap/envelope/","");  
  25.     Serializer->SoapAttribute("xsi""""http://www.w3.org/2001/XMLSchema-instance""xmlns");     
  26.     Serializer->SoapAttribute("xsd""""http://www.w3.org/2001/XMLSchema""xmlns");  
  27.     Serializer->SoapAttribute("soapenc","","http://schemas.xmlsoap.org/soap/encoding/","xmlns");  
  28.     Serializer->SoapAttribute("tns","","http://tempuri.org/","xmlns");  
  29.     Serializer->SoapAttribute("types","","http://tempuri.org/encodedTypes","xmlns");  
  30.     Serializer->StartBody(L"NONE");  
  31.     //Serializer->SoapAttribute("encodingStyle", "", "http://schemas.xmlsoap.org/soap/encoding/", "soap");  
  32.     Serializer->StartElement("GetExpertData","","http://tempuri.org/","tns"); // 4   
  33.     Serializer->StartElement("projectCode","","","");  
  34.     Serializer->SoapAttribute("xsi:type","","xsd:string","");  
  35.     Serializer->WriteString("CS201103250001");   
  36.     Serializer->EndElement();  
  37.     Serializer->EndElement();  
  38.     Serializer->EndBody();  
  39.     Serializer->EndEnvelope();  
  40.       
  41.     Connector->EndMessage();   
  42.       
  43.     Reader.CreateInstance(__uuidof(SoapReader30));  
  44.       
  45.     Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),"");  
  46.       
  47.     SetDlgItemText(IDC_EDIT1, (const char*)Reader->RpcResult->xml);  

 

 

以及部署在webservice上的内容

 

 

[xhtml]  view plain copy
  1. POST /WebSite3/serviceForVC.asmx HTTP/1.1  
  2. Host: localhost  
  3. Content-Type: text/xml; charset=utf-8  
  4. Content-Length: length  
  5. SOAPAction: "http://tempuri.org/GetExpertData"  
  6.   
  7. xml version="1.0" encoding="utf-8"?>  
  8. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://tempuri.org/" xmlns:types="http://tempuri.org/encodedTypes" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  9.   <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">  
  10.     <tns:GetExpertData>  
  11.       <projectCode xsi:type="xsd:string">stringprojectCode>  
  12.     tns:GetExpertData>  
  13.   soap:Body>  
  14. soap:Envelope>  

 

大家在自己查看有关soap协议时候,肯定会查到相关属性如何使用,我们可以对比一下上面的代码及xml对应关系就不难看出,实际上通过soap协议就是在传送一个xml内容,soap通过自身对象将webservice中的xml封装为自己的消息传递给服务器,等待服务器做出返回信息

 

由于我的例子是在本地操作的,webservice中尤其要注意一件事,因为目前流行.net框架,所以在.net创建webservice的工程时,不会自动添加关于soap的类说明

 

[WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.Protocols.SoapRpcService]

 

上面两行代码均在制定webservice的命名空间及定义soapRpcService.只有这样,VC6才能访问到webservice中的服务.

你可能感兴趣的:(webservice,c++,socket)