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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/delphiscn">int</int>
這裡我們可以填寫具體的x、y的數值 並提交查看返回結果 看是否有誤 如x可取10 y取6 單擊調用 他返回了一個XML頁面
<?xml version="1.0" encoding="utf-8" ?>
<int xmlns="http://blog.csdn.net/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" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
</p>
<p>
<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應用開發完畢