创建和使用 XML Web 服务

ylbtech-WebService-XML Web: 创建和使用 XML Web 服务

创建和使用 XML Web 服务。小案例实现 Web 服务的创建、发布和调用。

1.A,发布者(Promulgator) 返回顶部
发布者网站(The Publisher site web)
/App_Code/WebService.cs
View Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

using System.Data.SqlClient;
using System.Data;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/",Name="ylb科技",Description="专业的WebService服务")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    /// <summary>
    /// 带方法名注释 MessageName="欢迎某人"
    /// </summary>
    /// <returns></returns>
    [WebMethod(MessageName="欢迎某人")]
    public string HelloWorld2()
    {
        return "欢迎你伟大的ylb先生。";
    }

    /// <summary>
    /// 返回所有产品信息
    /// 用适配器做的,无参数
    /// </summary>
    /// <returns></returns>
    [WebMethod]
    public DataTable GetProducts()
    {
        string sql = "select * from Products";

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "server=.;database=northwind;integrated security=sspi";

        SqlDataAdapter adapter = new SqlDataAdapter(sql, con);

        con.Open();

        adapter.Fill(ds);

        con.Close();
        
        ///取出表
        dt = ds.Tables[0];

        return dt;
    }

    /// <summary>
    /// 返回产品集合,根据CategoryID
    /// 有参数
    /// </summary>
    /// <param name="cateID"></param>
    /// <returns></returns>
    [WebMethod(CacheDuration = 600)] //设置缓存以秒为单位
    public DataTable GetProductsByCateID(int cateID)
    {

        string sql = "select * from Products where categoryID=@categoryID";

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "server=.;database=northwind;integrated security=sspi";

        SqlCommand com = conn.CreateCommand();

        com.CommandText = sql;
        //配参
        com.Parameters.Add(new SqlParameter("@categoryID", cateID));

        conn.Open();
        SqlDataReader sdr = com.ExecuteReader();
        dt.Load(sdr);
        
        conn.Close();
        ds.Tables.Add(dt);

        dt = ds.Tables[0];

        return dt;
    }


    
}
1.B,调用者(Caller)返回顶部
调用者网站(The Caller site web)
1.B.0, 网站右键添加“添加 Web 引用” 【略】
/App_WebReferences/
/App_WebReferences/localhost/
/App_WebReferences/localhost/WebService.discomap
/App_WebReferences/localhost/WebService.disco
/App_WebReferences/localhost/WebService.wsdl
/Web.config【添加后 web 会添加这样一条注册】
<configuration>
    <appSettings>
        <add key="localhost.WebService" value="http://localhost:50864/WebSite/WebService.asmx"/>
    </appSettings>
</configuration>
1.B.1,
/DemoHouTai.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoHouTai.aspx.cs" Inherits="DemoHouTai" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            后台调用WebService应用</h3>
        <h2>
            方法:添加对象
        </h2>
        <hr />
        <h4>
            案例:调用查所有产品信息,无参数</h4>
        <asp:GridView ID="gvwProductList" runat="server" AllowPaging="True"
            EnableModelValidation="True" 
            onpageindexchanging="gvwProductList_PageIndexChanging">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
/DemoHouTai.aspx.cs
View Code
using System;
using System.Web.UI.WebControls;

public partial class DemoHouTai : System.Web.UI.Page
{
    /// <summary>
    /// 展示所有产品
    /// </summary>
    private void BindProductList()
    {
        gvwProductList.DataSource = new localhost.ylb科技().GetProducts();
        gvwProductList.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //调用
            BindProductList();
        }
    }
    protected void gvwProductList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //新页码
        gvwProductList.PageIndex = e.NewPageIndex;
        
        //更新数据
        BindProductList();
    }
}
1.B.2,
/DemoHouTai2.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoHouTai2.aspx.cs" Inherits="DemoHouTai2" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            后台调用WebService应用</h3>
        <h2>
            方法:添加对象
        </h2>
        <hr />
        <h4>
            案例:调用查所有产品信息,有参数</h4>
        <hr />
        请输入产品类别:<asp:TextBox ID="txtCategoryID" runat="server"></asp:TextBox>
        &nbsp;<asp:Button ID="btnSubmit" runat="server" Text="搜索" 
            onclick="btnSubmit_Click" />
            <div>
                <asp:Label ID="lblState" runat="server" ForeColor="Red" ></asp:Label></div>
        <hr />
        
        该分类下的产品是:<br />
        <asp:GridView ID="gvwProductList" runat="server" AllowPaging="True" 
            EnableModelValidation="True" 
            onpageindexchanging="gvwProductList_PageIndexChanging">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
/DemoHouTai2.aspx.cs
View Code
using System;
using System.Web.UI.WebControls;

public partial class DemoHouTai2 : System.Web.UI.Page
{
    /// <summary>
    /// 绑定产品类表,根据CategoryID
    /// </summary>
    private void BindProductList()
    {
        lblState.Text = string.Empty;
        
     int categoryID= 1;
        if (IsPostBack) //不是第一次加载该页面
        {
            if (txtCategoryID.Text.Length > 0)
            {
                categoryID= Convert.ToInt32(txtCategoryID.Text.Trim());
            }
        }

        gvwProductList.DataSource = new localhost.ylb科技().GetProductsByCateID(categoryID);
        gvwProductList.DataBind();
       
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            //调用
            BindProductList();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        BindProductList();
    }
    protected void gvwProductList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //当前页码
        gvwProductList.PageIndex = e.NewPageIndex;
        
        //更新数据
        BindProductList();
    }
}
1.B.3,
/DemoQianTai.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoQianTai.aspx.cs" Inherits="DemoQianTai" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h3>前台调用WebService应用</h3>
    <h2>
     方法:添加对象
    </h2>
    <hr />
    <h4>案例:调用查所有产品信息,无参数</h4>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            DataSourceID="ObjectDataSource1" EnableModelValidation="True">
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetProducts" TypeName="localhost.ylb科技">
        </asp:ObjectDataSource>
    </div>
    </form>
</body>
</html>
1.B.4,
/DemoQianTai2.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoQianTai2.aspx.cs" Inherits="DemoQianTai2" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            前台调用WebService应用</h3>
        <h2>
            方法:添加对象
        </h2>
        <hr />
        <h4>
            案例:调用查所有产品信息,有参数</h4>
            <hr />
           请输入产品类别:<asp:TextBox ID="txtCategoryID" runat="server"></asp:TextBox>
           &nbsp;<asp:Button  ID="btnSubmit" runat="server" Text="搜索" />
           <hr />
           该分类下的产品是:<br />
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            DataSourceID="ObjectDataSource1" EnableModelValidation="True">
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetProductsByCateID" TypeName="localhost.ylb科技">
            <SelectParameters>
                <asp:ControlParameter ControlID="txtCategoryID" DefaultValue="1" Name="cateID" 
                    PropertyName="Text" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>
    </div>
    </form>
</body>
</html>
1.C,资源下载(Free Download)返回顶部

https://files.cnblogs.com/ylbtech/XmlWeb-WebService-WebForm-PromulgatorAndCaller.rar

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ylbtech/p/3009099.html