开发平台: WinXP SP3+IIS5.1+IE7+Visual Studio 2005+Visual Studio 6.0+ MS SOAP Toolkit 3.0 一.创建ASP.NET WebService Visual Studio 2005à文件à新建à网站àASP.NET Web服务项目WebServiceDemo。 以下为Service.cs代码清单: using System; using System.Web; using System.ComponentModel; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://Fantasy/WebServiceDemo/")] [System.Web.Services.Protocols.SoapRpcService] public class Service : System.Web.Services.WebService { public Service() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public int AddNumbers(int NumberOne,int NumberTwo) { return NumberOne + NumberTwo; } } 生成à生成网站à发布网站。 二.为WebServiceDemo创建虚拟目录WebServiceDemo 开始à设置à控制面板à管理工具àInternet信息服务à默认网站à右击新建à虚拟目录WebServiceDemo指向Projects\WebServiceDemo\PrecompiledWeb\WebServiceDemo. 三.在Web浏览器中输入http://localhost/WebServiceDemo/Service.asmx 出现以下错误: 无法显示 XML 页。 使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 名称以无效字符开头。处理资源 'http://localhost/WebServiceDemo/Service.asmx' 时出错。第 1 行,位置: 2 <%@ WebService Class="Service" %> -^ 解决方案:http://blog.csdn.net/changyuming/archive/2008/09/24/2971400.aspx 运行C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe –i 在cmd窗口中将显示: 开始安装ASP.NET <2.0.50727>。 …………
若要使(局域网)外网访问WebServiceDemo/Service.asmx,则需要为Windows防火墙添加80端口的例外TCP80。在局域网内其他机器的Web浏览器中输入:http://192.168.89.125/WebServiceDemo/Service.asmx则可访问本机的Service.asmx。
若出现“此 Web 服务不符合 WS-I Basic Profile v1.1。”错误,则在Web.Config文件中<system.web></system.web>之间添加以下代码:
<!--解决“此Web服务不符合 WS-I Basic Profile v1.1。”错误-->
<webServices>
<conformanceWarnings>
<remove name='BasicProfile1_1'/>
</conformanceWarnings>
</webServices>
四.创建VC6.0客户端SOAP应用程序远程调用AddNumbers函数
参考SOAP Toolkit 3.0 User Guideà Basic Sample Applicationsà Using the Low-Level APIs for SOAP Messages (DocSample2) à Creating a C++ Version of DocSample2à Creating the Client-Side C++ Code
以下为Win32 Console项目SoapDemo的soapDemo.cpp代码清单:
// soapDemo.cpp
#include <stdio.h>
#include <stdlib.h>
//先导入msxml4.dll,再导入mssoap30.dll
#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;
void Add(int No1,int No2)
{
//char *_itoa( int value, char *string, int radix );
char str1[10],str2[10];
itoa(No1,str1,10); itoa(No2,str2,10);
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = "http://localhost/WebServiceDemo/Service.asmx"; Connector->Connect();
// Begin the message.
Connector->Property["SoapAction"] = "http://Fantasy/WebServiceDemo/AddNumbers";
Connector->BeginMessage();
// Create the SoapSerializer object.
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// Connect the serializer object to the input stream of the connector object.
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// Build the SOAP Message.
Serializer->StartEnvelope("","","");
//这里可以添加SoapHeader验证代码:
/*
Serializer->StartHeader("");
Serializer->StartHeaderElement("MyHeader","http://Fantasy/WebServiceDemo/",0,"","NONE","");
Serializer->StartElement("UserName","","","");//填充参数2则出错。
Serializer->WriteString(_bstr_t(userName));
*/
Serializer->StartBody("");
Serializer->StartElement("AddNumbers","http://Fantasy/WebServiceDemo/","","");
//(第三个参数可有可无,若加上第二个参数出错!?)
Serializer->StartElement("NumberOne","","","");// NumberOne=
Serializer->WriteString((_bstr_t)str1);
Serializer->EndElement();
Serializer->StartElement("NumberTwo","","","");// NumberOne=
Serializer->WriteString((_bstr_t)str2);
Serializer->EndElement();
Serializer->EndElement();
//
Serializer->EndBody();
Serializer->EndEnvelope();
// Send the message to the XML Web service.
Connector->EndMessage();
// Read the response.
Reader.CreateInstance(__uuidof(SoapReader30));
// Connect the reader to the output stream of the connector object.
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
// Display the result.
printf("Answer = %s\n", (const char*)Reader->RpcResult->text);
}
int main()
{
CoInitialize(NULL);
Add(12,24);
CoUninitialize();
return 0;
}
五.SoapDemo客户端调用AddNumbers执行结果:Answer=36
参考:
《SOA的发展历史与标准规范》
http://soft.zdnet.com.cn/software_zone/2009/0204/1332793.shtml
《WebService简介》
http://www.cnblogs.com/bjzhanghao/archive/2004/08/04/30012.html
《ASP.NET Web Service如何工作》http://msdn.microsoft.com/zh-cn/library/ms996410.aspx
《在vc6里soap调用WebService》http://www.cnblogs.com/arui/archive/2006/06/15/426615.html
《使用webservice解决多系统登陆问题》
http://www.dezai.cn/article_show.asp?ArticleID=16678
在线WebService:http://www.webxml.com.cn/zh_cn/web_services.aspx
介绍几个外文学习网站:
Whatis-http://whatis.techtarget.com/
SoapUser-http://www.soapuser.com/
Oreilly-http://oreilly.com/
w3schools-http://www.w3schools.com/
评论