Web网络编程第八次试验:Web服务

本文档展示了如何进行Web服务调用,包括查询火车运行时刻表及自定义四则运算Web服务的实现。实验中,通过Visual Studio 2019和ASP.NET Framework创建了网页,用户输入起始站和终点站查询火车信息,以及输入数字和运算符进行计算。实验旨在提升对网络服务调用和自定义Web服务的熟练度。
摘要由CSDN通过智能技术生成

前言

为了帮助同学们完成痛苦的实验课程设计,本作者将其作出的实验结果及代码贴至CSDN中,供同学们学习参考。如有不足或描述不完善之处,敬请各位指出,欢迎各位的斧正!

一、实验目的

1、掌握调用网络中提供的服务;
2、熟练地调用自定义的Web服务。

二、实验环境

Visual Studio 2019 ASP.Net Framework

三、实验内容

1、调用网上提供的火车运行时刻表Web服务。要求如下:
(1)通过网络获得该服务的网址。
(2)用户输入起始站和终点站后,查询相关火车车次的信息。
在这里插入图片描述

2、调用自定义的Web服务。要求如下:
(1)定义一个实现四则运算的Web服务,可以接受页面用户输入的数字和运算符号。
(2)在后台代码中调用该服务后显示运算的结果。
在这里插入图片描述

四、代码及截图

Default.aspx源代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>列车时刻查询</title>
    <link href="~/style/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="page">
            <div class="header">
                <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0" style="height:440px">
                    <tr>
                        <td class="style3">
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td class="style3">
                            <img src="4.png" alt="WebXmlLogo" border="0" style="width:960px;height:190px;" /> 
                        </td>
                    </tr>
                    <div class="title">
                        <tr>
                            <td align="center" class="style3">
                                <strong>
                                    &nbsp;<h2><font color="yellow">欢迎进入列车查询程序</font></h2>
                                </strong>
                            </td>
                        </tr>
                    </div>
                    <div>
                        <tr>
                            <td class="style3">
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td class="style3">
                                <table border="0" cellspacing="0" cellpadding="0" style="width:100%">
                                    <tr>
                                        <td style="height:25px;table-layout:auto;border-left-color:yellow;border-bottom-color:yellow;cursor:move;border-top-style:ridge;
border-top-color:yellow;border-right-style:ridge;border-left-style:ridge;border-collapse:separate;border-right-color:yellow;border-bottom-style:ridge;">
                                            发车站&nbsp;&nbsp;<asp:TextBox Columns="10" CssClass="input1" ID="textStartStation" MaxLength="10" runat="server" OnTextChanged="textStartStation_TextChanged" Width="80px" ></asp:TextBox>
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;到达站
                                                           
                                            <asp:TextBox Columns="10" CssClass="input1" ID="textArriveStation" MaxLength="10" runat="server" Width="80px"></asp:TextBox>
                                            &nbsp;
                                            <asp:Button CssClass="input2" ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" Height="19px" />
                                            <tr>
                                                <td style="height:25px;table-layout:fixed;border-left-color:yellow;border-bottom-color:yellow;cursor:move;border-top-style:ridge;border-top-color:yellow;border-right-style:ridge;border-left-style:ridge;border-collapse:separate;border-right-color:yellow;border-bottom-style:ridge;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox Columns="10" CssClass="input1" ID="TextTrainCode" MaxLength="10" runat="server" Width="80px"></asp:TextBox>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                    <asp:Button CssClass="input2" ID="Button2" runat="server" Text="查询" OnClick="Button2_Click" Height="19px" /> 
                                                </td>
                                            </tr>
                                        </td> 
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </div>
                    <div>
                        <tr>
                            <td class="style2">
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td class="style3">
                                <table border="0" cellpadding="2" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#CCFFFF" style="width:960px;">
                                    <tr>
                                        <th style="background-color:#FFFF00" bgcolor="#CCFFFF" align="left">车次</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">始发站</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">终点站</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">发车站</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">发车时间</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">到达站</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">到达时间</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">历程(KM)</th>
                                        <th style="width:10%;background-color:#FFFF00;bgcolor" bgcolor="#CCFFFF" align="left">历时</th>
                                    </tr>
                                    <asp:Repeater runat="server" ID="Repeater1" EnableViewState="false" OnItemDataBound="Repeater1_ItemDataBound">
                                        <ItemTemplate>
                                            <tr>
                                                <td class="tdbg">
                                                    <asp:HyperLink ID="TrainCode" runat="server" Target="_blank" Font-Bold="true"></asp:HyperLink>
                                                </td>
                                                <td class="tdbg" style="white-space:nowrap;"><%#Eval("FirstStation") %></td>
                                                <td class="tdbg" style="white-space:nowrap;"><%#Eval("LastStation") %></td>
                                                <td class="tdbg" style="white-space:nowrap;"><%#Eval("StartStation") %></td>
                                                <td class="tdbg"><%#Eval("StartTime") %></td>
                                                <td class="tdbg"><%#Eval("ArriveStation") %></td>
                                                <td class="tdbg"><%#Eval("ArriveTime") %></td>
                                                <td class="tdbg"><%#Eval("KM") %></td>
                                                <td class="tdbg"><%#Eval("UseDate") %></td>
                                            </tr>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td class="style1">
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td class="style2">
                                <asp:Label ID="Lable1" runat="server"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td class="style2">
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td align="center" class="style2"></td>
                        </tr>
                        <tr>
                            <td class="style3">
                                &nbsp;
                            </td>
                        </tr>
                    </div>
                </table>

            </div>
        </div>
    </form>
</body>
</html>

其设计界面如下:
在这里插入图片描述

Default.aspx.cs如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{

    cn.com.webxml.ws.TrainTimeWebService train = new cn.com.webxml.ws.TrainTimeWebService();    
    DataSet ds = new DataSet();        
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
           /* Lable1.Text = train.getVersionTime();*/
        }
    }
    protected void textStartStation_TextChanged(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        
        string start = textStartStation.Text;
        string end = textArriveStation.Text;
        ds = train.getStationAndTimeByStationName(start, end, "");
        Repeater1.DataSource = ds.Tables[0].DefaultView;
        Repeater1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string code = TextTrainCode.Text;
        ds = train.getStationAndTimeDataSetByTrainCode(code, "");
        Repeater1.DataSource = ds.Tables[0].DefaultView;
        Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            HyperLink oHyperLink = (HyperLink)e.Item.FindControl("TrainCode");
            string TrainCode = DataBinder.Eval(e.Item.DataItem, "TrainCode").ToString();
            oHyperLink.Text = TrainCode;
            string FirstStation = DataBinder.Eval(e.Item.DataItem, "FirstStation").ToString();
            string StartStaion = DataBinder.Eval(e.Item.DataItem, "StartStation").ToString();
            string ArriveStation = DataBinder.Eval(e.Item.DataItem, "ArriveStation").ToString();
            if (FirstStation.Contains("没有发现"))
            {
                oHyperLink.ToolTip = FirstStation;
                oHyperLink.NavigateUrl = "";
            }
            else
            {
                oHyperLink.ToolTip = "获得" + TrainCode + "详细信息";
                oHyperLink.NavigateUrl = "Detail.aspx?id=" + Server.UrlEncode(TrainCode) + "&Start=" + Server.UrlEncode(StartStaion) + "&Arrive=" + Server.UrlEncode(ArriveStation);
            }
        }
    }
}

程序运行截图:
初始界面:
在这里插入图片描述
输入北京、上海,单击查询:
在这里插入图片描述
输入G2021,单击查询:
在这里插入图片描述

2.calculator.aspx源代码如下:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Width="80px"></asp:TextBox>
&nbsp;
            <asp:DropDownList ID="DropDownList1" runat="server" Width="40px">
                <asp:ListItem>+</asp:ListItem>
                <asp:ListItem>-</asp:ListItem>
                <asp:ListItem>*</asp:ListItem>
                <asp:ListItem>/</asp:ListItem>
            </asp:DropDownList>
&nbsp;
            <asp:TextBox ID="TextBox2" runat="server" Width="80px"></asp:TextBox>
&nbsp;
            <asp:Button ID="Button1" runat="server" Text="=" Width="30px" OnClick="Button1_Click" />
&nbsp;
            <asp:TextBox ID="TextBox3" runat="server" Width="80px"></asp:TextBox>
        </div>
    </form>
</body>
</html>

其设计界面如下:
在这里插入图片描述
Calculator.aspx.cs设计如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace calculator
{
    public partial class calculator : System.Web.UI.Page
    {
        decimal a,b;
        string str;
        protected void Button1_Click(object sender, EventArgs e)
        {
            calculator1 calculator1 = new calculator1();
           
            a = Convert.ToInt32(TextBox1.Text.Trim());
            b = Convert.ToInt32(TextBox2.Text.Trim());
            str = DropDownList1.SelectedValue.Trim();
            TextBox3.Text = calculator1.cal(a, b, str).ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {            
        }        
    }
}

Cal.asmx.cs设计如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace calculator
{
    /// <summary>
    /// calculator1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class calculator1 : System.Web.Services.WebService
    {
        [WebMethod]
        public decimal cal (decimal a,decimal b,string str)
        {
            if (str == "+")
                return a + b;
            else if (str == "-")
                return a - b;
            else if (str == "*")
                return a * b;
            else if (str == "/" && b != 0)
                return a / b;
            else if (str == "/" && b == 0)
                return -99999999;
            else return -99999999;
        }        
    }
}

运行界面如图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、实验总结

通过本次实验,我简要地掌握了调用网络中提供的服务、较为熟练地调用了自定义的Web服务。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Knight_V_Schumacher

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值