WS(webService) 的简单应用、实战案例

web service的简单应用

一:.主要实现四大大功能

           1.在解决方案中创建一个webservice,并且调用

           2.调用互联网上的webservice,(火车时刻表查询)

           3.获取手机号码信息、判断qq号码是否在线

1.在解决方案中创建一个webservice,并且调用

service.asmx前台代码:

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>

 

service.cs后台代码:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

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

 

  //程序生成的默认方法  

  [WebMethod]
    public string HelloWorld() {
        return "我的第一个webservice程序";
    }
   
}

 

 createWs.aspx前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="createWs.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>web服务的创建</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lbText" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

上面放置了一个label服务器控件。用来接收webService返回值。

 

create.aspx.cs后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        localhost.Service xx = new localhost.Service();  //localhost:引用时的命名空间   ,.service(创建ws时的名称)

        lbText.Text = xx.HelloWorld();  //调用ws里面的一个方法
    }
}

测试效果:


 

2.调用互联网上的webservice,(火车时刻表查询)

 1.引用互联网上的火车时刻表ws方法:

 

具体地址我分享一下:http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx

点击进去可以看到很多方法,

  我们具体引用两个方法:

    1.getStationName()   //获取城市名称

    2.getStationAndTimeByStationName(DropDownList1.Text, DropDownList2.Text, "")   //通过始发->目的 ,返回一个dateset

 

TrainWs.aspx前台代码:

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

<!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">
    <h4>火车时刻表查询</h4>
        起始站:<asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        终点站<asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="查询" οnclick="Button1_Click" />
        <br /><br />
        <asp:GridView ID="GridView1" runat="server" BackColor="White"
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
            GridLines="Vertical">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
   
    </form>
</body>
</html>

 

TrainWs.aspx.cs后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TrainWs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        localhost.TrainTimeWebService lt = new localhost.TrainTimeWebService();
        string[] str = lt.getStationName();
        for (int i = 0; i < str.Length; i++)
        {
            DropDownList1.Items.Add(str[i]);
            DropDownList2.Items.Add(str[i]);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.TrainTimeWebService lt = new localhost.TrainTimeWebService();
        GridView1.DataSource = lt.getStationAndTimeByStationName(DropDownList1.Text, DropDownList2.Text, "");
        GridView1.DataBind();
    }
}

 

运行效果:

 

3.获取手机号码信息、判断qq号码是否在线 

1.myTest.aspx前台代码:

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

<!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>
    <script type="text/javascript">
      function myTest()
      {
        MobileCodeWS.getMobileCodeInfo(document.getElementById('txtPhone').value,'',onSuccess);
      }
     
      function onSuccess(result)
      {
        document.getElementById('mess').innerHTML=result;
      }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     查询手机号码所在地:<hr /><br />
     手机号码:<asp:TextBox ID="txtPhone" runat="server">    
     </asp:TextBox><asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" /><br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <br />
    查询qq是否在线:<hr /><br />
        请输入qq号码:<asp:TextBox ID="txtQq" runat="server"></asp:TextBox><asp:Button ID="Button2"
            runat="server" Text="查询" OnClick="Button2_Click" />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>

 

2.myTest.aspx.cs后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class myTest: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.MobileCodeWS lh = new localhost.MobileCodeWS();
        Label1.Text = lh.getMobileCodeInfo(txtPhone.Text, "");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        localhost.qqOnlineWebService qq = new localhost.qqOnlineWebService();
        string info = qq.qqCheckOnline(txtQq.Text);
        if (info == "Y") {
            Label2.Text = "该号码在线!";
        } else {
            Label2.Text = "该号码不在线!";
        }
    }
}

 

 

效果示意图:


 

 

 

希望有ws经验的人,可以一起分享一下,以上内容有什么不足之处,还请高手们多多请教》。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值