ASP.NET webservice,查询天气预报,手机号码等

本文介绍了如何构建和集成Web服务,包括实现方法、传输协议(HTTP-GET、HTTP-POST与SOAP)、以及如何将WebService发布到服务器上,并通过实例展示了如何调用Web服务进行加法运算和获取学生数据。此外,还涉及了使用特定API获取天气预报、电话号码查询等应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

web服务:
添加方法:
[WebMethod]//必须有
public int Add(int a,int b)
{
    returna+b;
}

public Data GetStudent()
{
   string str=System.ConigurationManager.
   using(SqlConnection cnn=new SqlConnection())
     {
         using(SqlCommand cmm=cnn.CreateCommand())
            {
                cmm.CommandText="select*from student";
                SqlDataAdapter da =new SqlDataAdapter(cmm);
                DataSet ds =new DataSet();
                da.Fill(ds);
                return ds;
            }
     }
}

web服务的传输协议知识点:
HTTP-GET
在地址栏

<webService><protocols><add name="HttpGet"></protocols></webService>
HTTP-POST
SOAP
将WebService发布到服务器上:

 

调用上述加法(新建网站)(没有发布必须打开需要调用的网站)

 protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.WebService ws=new localhost.WebService();
        this.TextBox3.Text = ws.Add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text)).ToString();

        this.GridView1.DataSource=ws.GetStudent();
        this.GridView1.DataBind();
    }


天气预报:

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!this.IsPostBack)
           {
              cn.com.webxml.www.WeatherWebService wws=new cn.com.webxml.www.WeatherWebService();
              this.DropDownList1.DataSource=wws.getSupportProvince();
              this.DropDownList1.DataBind();
           }
    }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();
        BindCity(wws);
    }
    private void BindCity(cn.com.webxml.www.WeatherWebService wws)
    {
        this.DropDownList2.Items.Clear();
        string[] strs = wws.getSupportCity(this.DropDownList1.SelectedValue);
        foreach (string str in strs)
        {
            this.DropDownList2.Items.Add(new ListItem(str.Substring(0, str.IndexOf("("))));
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();
        string[] strs = wws.getWeatherbyCityName(this.DropDownList2.SelectedValue);
        this.Label1.Text = strs[0] + "," + strs[1] + "<br/>";
        this.Label1.Text = "温度:" + strs[5] + "<br/>";
        this.Label1.Text = "风:" + strs[7];
    }

电话号码的查询:
    protected void Button3_Click(object sender, EventArgs e)
    {
        cn.com.webxml.webservice.MobileCodeWS ws=new cn.com.webxml.webservice.MobileCodeWS ();
        string strss = ws.getMobileCodeInfo(this.TextBox4.Text, this.TextBox5.Text);
        this.Label2.Text = strss;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值