Web服务快速使用

在《常用的Web服务收集》(http://blog.csdn.net/KingWolfOfSky/archive/2010/04/10/5470780.aspx)一文中收集了相关的Web服务,本文用一个简单的例子来说明如何使用Web服务。
--------------------------------------------------------------------------------

首先笔者创建了一个ASP.NET网站,如下图:

ASP.NET网站解决方案 

然后右键单击解决方案,选择“添加Web引用”,如下图

添加web引用 

然后在弹出的对话框中添加你需要的web服务,这里添加的是火车时刻表 WEB 服务 (http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx),如下图, 

添加web引用
在这里你需要添加一个Web引用名作为命名空间

然后在Web页面上添加几个简单的控件,如下

添加控件 

页面部分代码如下

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

<!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>
<asp:Label ID="Label1" runat="server" Text="起始站"></asp:Label>
<asp:TextBox ID="Start" runat="server" Style="width: 128px" mce_Style="width: 128px"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="终点站"></asp:Label>
<asp:TextBox ID="End" runat="server"></asp:TextBox>
<br />
<asp:Button ID="BtnGet" runat="server" OnClick="BtnGet_Click" Text="获取火车信息" />
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE"
BorderStyle
="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical"
PageSize
="5">
<RowStyle BackColor="#F7F7DE" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
</div>
</form>
</body>
</html>

然后添加“获取火车信息”按钮的事件响应:

代码
protected void BtnGet_Click(object sender, EventArgs e)
{
Webservice.TrainTimeWebService myTrainTimeWebService
= new Webservice.TrainTimeWebService();
GridView1.DataSource
= myTrainTimeWebService.getStationAndTimeByStationName(Start.Text,End.Text,"");
GridView1.DataBind();
}


本文来自CSDN博客,转载请标明出处:http:
//blog.csdn.net/KingWolfOfSky/archive/2010/04/11/5473545.aspx

这里通过

getStationAndTimeByStationName

通过发车站和到达站查询火车时刻表 DataSet

输入参数:StartStation = 发车站,ArriveStation = 到达站(支持第一个字匹配模糊查询),空字符串默认发车站上海和到达站北京,UserID = 商业用户ID(普通用户不需要);返回数据:DataSet,Item.(TrainCode)=车次、Item.(FirstStation)=始发站、Item.(LastStation)=终点站、Item.(StartStation)=发车站、Item.(StartTime)=发车时间、Item.(ArriveStation)=到达站、Item.(ArriveTime)=到达时间、Item.(KM)=里程(KM)、Item.(UseDate)=历时

来获取信息

最后测试运行如下:

火车信息查询


--------------------------------------------------------------------------------
如果您想要添加自己的Web服务,《添加自己的Web服务》一文中做了简要的介绍。 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/KingWolfOfSky/archive/2010/04/11/5473545.aspx

posted @ 2010-04-19 02:10  狼の禅  阅读(506)  评论(0编辑  收藏  举报
我要啦免费统计