前言
为了帮助同学们完成痛苦的实验课程设计,本作者将其作出的实验结果及代码贴至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">
</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>
<h2><font color="yellow">欢迎进入列车查询程序</font></h2>
</strong>
</td>
</tr>
</div>
<div>
<tr>
<td class="style3">
</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;">
发车站 <asp:TextBox Columns="10" CssClass="input1" ID="textStartStation" MaxLength="10" runat="server" OnTextChanged="textStartStation_TextChanged" Width="80px" ></asp:TextBox>
到达站
<asp:TextBox Columns="10" CssClass="input1" ID="textArriveStation" MaxLength="10" runat="server" Width="80px"></asp:TextBox>
<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;">
车 次 <asp:TextBox Columns="10" CssClass="input1" ID="TextTrainCode" MaxLength="10" runat="server" Width="80px"></asp:TextBox>
<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">
</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">
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Lable1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
</td>
</tr>
<tr>
<td align="center" class="style2"></td>
</tr>
<tr>
<td class="style3">
</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>
<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>
<asp:TextBox ID="TextBox2" runat="server" Width="80px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="=" Width="30px" OnClick="Button1_Click" />
<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服务。