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

来源:互联网 发布:黑马程序员好找工作吗 编辑:程序博客网 时间:2024/05/17 01:39
 

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;
    }

 

原创粉丝点击