A Introduction to Create a WebService using .NET

本文介绍如何使用.NET在Visual Studio 2008中创建一个简单的Web服务应用,并演示了如何通过编写后台函数实现两个整数的减法运算,同时展示了SOAP 1.1和1.2、HTTP POST请求及响应示例。

A Introduction to Create a WebService using .NET

严竞雄
JingxiongYan@hotmail.com

首先 在VS2008中新建一个ASP.NET Web服务应用程序 将工程取名为WebService

将Service1.asmx中的代码修改如下

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService
{
/// <summary>
/// Service1 summary
/// </summary>
[WebService(Namespace = "http://blog.youkuaiyun.com/delphiscn")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

[WebMethod(Description="Return a Value")]
public int sub(int x,int y)
{
return x-y;
}

private void InitializeComponent()
{

}
}
}

在这里 我们定义了一个sub函数 用来取x、y的值 并将其相减结果返回

好了 现在后台的函数调用写好了 先来测试一下 在菜单栏中选择调试、启动调试 或者直接按F5 当出现消息页面后 选择单击sub

SOAP 1.1
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: http://blog.youkuaiyun.com/delphiscn/sub

<?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:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<sub xmlns="http://blog.youkuaiyun.com/delphiscn">
<x>int</x>
<y>int</y>
</sub>
</soap:Body>
</soap:Envelope>


HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<subResponse xmlns="http://blog.youkuaiyun.com/delphiscn">
<subResult>int</subResult>
</subResponse>
</soap:Body>
</soap:Envelope>

SOAP 1.2
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<sub xmlns="http://blog.youkuaiyun.com/delphiscn">
<x>int</x>
<y>int</y>
</sub>
</soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<subResponse xmlns="http://blog.youkuaiyun.com/delphiscn">
<subResult>int</subResult>
</subResponse>
</soap12:Body>
</soap12:Envelope>

HTTP POST
以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx/sub HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

x=string&y=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://blog.youkuaiyun.com/delphiscn">int</int>

这里我们可以填写具体的x、y的数值 并提交查看返回结果 看是否有误 如x可取10 y取6 单击调用 他返回了一个XML页面

<?xml version="1.0" encoding="utf-8" ?>
<int xmlns="http://blog.youkuaiyun.com/delphiscn">4</int>

到此 一个完整的WebService应用已经写好并测试完毕 接下来我们可以自己开发一个前台来接受用户输入 并自己调用后来的服务

在项目中新建一个Web窗体 画上三个TextBox和一个Button 具体代码如下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebService.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Delphiscn Text Page</title>
<style type="text/css">
#Text1
{
width: 145px;
}
#Text2
{
width: 160px;
}
#form1
{
height: 348px;
}
</style>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Button1_onclick() {

}

// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 0px">

</div>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text="Button" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</p>
<p>
&nbsp;<asp:Image ID="Image1" runat="server" Height="101px" Width="107px" />
</p>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</form>
</body>
</html>

在WebForm1.aspx.cs中加入如下代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebService
{
public partial class WebForm1 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click1(object sender, EventArgs e)
{
WebService.Service1 my = new WebService.Service1();
int x = int.Parse(TextBox1.Text);
int y = int.Parse(TextBox2.Text);
int z = my.sub(x, y);
TextBox3.Text = z.ToString();
}

}
}

至此 一个完整的WebService应用开发完毕

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值