Web Service王者之道——之Hello World
使用Web Serveices最大的意义是提供统一的标准,只要在互联网上,你就可以享受众多按标准定制的服务,而不受客户端技术的限制。Web Service使得客户端和服务器端的耦合大大的降低。正是由于高度的标准化我们才能实现这些优点。在接下去的几天我将介绍一下如何用.NET来实现Web Services。之所以选择.NET是因为开发简单,容易理解。也只有自己动手实践才能更好的对Web Service有个深刻的理解。
使用C#来实现一个Hello World的Web Service你甚至都不需要写一句话。简单吧。
首先打开Visual Studio.net 2003,你没有?那请先去下一个装上吧。我是从Emule上下的英文版的,所以界面全是英文的,大家对付着看吧。
1、打开Visual Studio.Net 2003从菜单里新建一个Project,打开了新建Project的窗口。当然我们要做的是建立一个Web Service,所以在Templates里选ASP.NET Service,然后在Location里输入这个Project名称,咱们就叫HelloWorld吧。
接着点确定按钮,Visual Studio.Net将在本地的IIS里建立一个虚拟目录,如图1-2。
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5

// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }去掉注释后就是:
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>上面显示的XML就是返回的结果啦。
怎么样,非常简单吧。当然这个只是非常简单的演示了如何建立一个没有任何用处的Web Service而且,如果有兴趣就大家一起来讨论讨论,慢慢的在写一些复杂一点的教程吧。
使用C#来实现一个Hello World的Web Service你甚至都不需要写一句话。简单吧。
首先打开Visual Studio.net 2003,你没有?那请先去下一个装上吧。我是从Emule上下的英文版的,所以界面全是英文的,大家对付着看吧。
1、打开Visual Studio.Net 2003从菜单里新建一个Project,打开了新建Project的窗口。当然我们要做的是建立一个Web Service,所以在Templates里选ASP.NET Service,然后在Location里输入这个Project名称,咱们就叫HelloWorld吧。

图1-1
接着点确定按钮,Visual Studio.Net将在本地的IIS里建立一个虚拟目录,如图1-2。

图1-2 建立虚拟目录
2、等建立虚拟目录完成后,Visual Studio.Net 2003界面如图1-3所示,我们点击转到代码视图。

图1-3 设计界面
现在我们看见的就是由Visual Studio.Net 2003的ASP.NET Web Service模板生成的代码页,如图1-4。

图1-4代码编辑页
3、添加HelloWorld函数,这个函数VS.Net的模板已经带了,咱们只要把注释去掉就是了。
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5
// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}这样就完成一个最简单的Hello World的Web Service设计了。简单吧。现在让我们来看看效果吧。
点工具条上的运行按钮,如图1-5运行Web Service。

图1-5 运行Web Service
如果一切操作都没有错误的话,那么将打开一个Web Service的页面如图1-6。

图1-6 调用Web Service的页面
点Hello World链接就可以转到调用Hello World这个Service里的HelloWorld函数的页面了如图1-7。

图1-7 HelloWorld函数调用页面
点击调用按钮以Http Post的方式来调用该函数,试试看。
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>怎么样,非常简单吧。当然这个只是非常简单的演示了如何建立一个没有任何用处的Web Service而且,如果有兴趣就大家一起来讨论讨论,慢慢的在写一些复杂一点的教程吧。

浙公网安备 33010602011771号