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协议,理解完成后可以继续看以下代码.
- void SoapTestDlg::OnOutput()
- {
- CoInitialize(NULL);
- ISoapSerializerPtr Serializer;
- ISoapReaderPtr Reader;
- ISoapConnectorPtr Connector;
-
- HRESULT hr=Connector.CreateInstance(__uuidof(HttpConnector30));
- if(!SUCCEEDED(hr))
- {
- AfxMessageBox("error");
- return;
- }
-
- Connector->Property["EndPointURL"] = "http://localhost:1036/WebSite3/serviceForVC.asmx";
- Connector->Connect();
-
- 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
- Connector->BeginMessage();
- Serializer.CreateInstance(__uuidof(SoapSerializer30));
- Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
-
-
- Serializer->StartEnvelope("soap","http://schemas.xmlsoap.org/soap/envelope/","");
- Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");
- Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");
- Serializer->SoapAttribute("soapenc","","http://schemas.xmlsoap.org/soap/encoding/","xmlns");
- Serializer->SoapAttribute("tns","","http://tempuri.org/","xmlns");
- Serializer->SoapAttribute("types","","http://tempuri.org/encodedTypes","xmlns");
- Serializer->StartBody(L"NONE");
-
- Serializer->StartElement("GetExpertData","","http://tempuri.org/","tns"); // 4
- Serializer->StartElement("projectCode","","","");
- Serializer->SoapAttribute("xsi:type","","xsd:string","");
- Serializer->WriteString("CS201103250001");
- Serializer->EndElement();
- Serializer->EndElement();
- Serializer->EndBody();
- Serializer->EndEnvelope();
-
- Connector->EndMessage();
-
- Reader.CreateInstance(__uuidof(SoapReader30));
-
- Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),"");
-
- SetDlgItemText(IDC_EDIT1, (const char*)Reader->RpcResult->xml);
以及部署在webservice上的内容
- POST /WebSite3/serviceForVC.asmx HTTP/1.1
- Host: localhost
- Content-Type: text/xml; charset=utf-8
- Content-Length: length
- SOAPAction: "http://tempuri.org/GetExpertData"
-
- <?xml version="1.0" encoding="utf-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/">
- <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <tns:GetExpertData>
- <projectCode xsi:type="xsd:string">string</projectCode>
- </tns:GetExpertData>
- </soap:Body>
- </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中的服务.