WebService---web服务的使用

来源:互联网 发布:首席数据官联盟 编辑:程序博客网 时间:2024/05/18 07:39

一,什么是web服务

        Web服务(Web Service)是基于XML和HTTPS的一种服务,其通信协议主要基于SOAP,服务的描述通过WSDL,通过UDDI来发现和获得服务的元数据。
         可扩展的标记语言(XML)是Web service平台中表示数据的基本格式。除了易于建立和易于分析外,XML主要的优点在于它既是平台无关的,又是厂商无关的。 SOAP规范定义了SOAP消息的格式,以及怎样通过HTTP协议来使用SOAP。SOAP也是基于XML和XSD的,XML是SOAP的数据编码方式。Web service描述语言(WSDL)就是这样一个基于XML的语言,用于描述Web service及其函数、参数和返回值。


      SOAP:最初是简单对象访问协议(Simple Object Access Protocol),SOAP 定义一个 XML 文档格式,该格式描述如何调用一段远程代码的方法

        WSDL:Web 服务描述语言(Web Services Description Language)是一个描述 Web 服务的 XML 词汇表。

     UDDI:统一描述、发现和集成(Universal Description, Discovery, and Integration)协议向 Web 服务注册中心定义 SOAP 接口。

        使用web服务的好处:

                      1 跨防火墙的通信
                      2 应用程序集成
                      3 B2B的集成
                      4 软件和数据重用

        web服务提供网址:www.webxml.com.cn

二,使用web服务
 (1)web应用程序步骤:在web应用程序上右键---添加服务应用---高级---添加web引用---输入URL(下图)
 (2)调用web服务代码:
   protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Items.Add("--请选择省--");
           
            cn.com.webxml.webservice.WeatherWS ws = new WebServiceTest.cn.com.webxml.webservice.WeatherWS();
            string[] strProvices = ws.getRegionProvince(); //获得所有的省份:每项中:湖北,31117

            for (int i = 0; i <strProvices.Length; i++)
            {
                string[] strs = strProvices[i].Split(',');
                string provinceName = strs[0];
                DropDownList1.Items.Add(provinceName);
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (DropDownList1.SelectedIndex > 0)
            {
                string strProviceName = DropDownList1.Text; //获得用户选中的省份
                cn.com.webxml.webservice.WeatherWS ws = new WebServiceTest.cn.com.webxml.webservice.WeatherWS();
                string[] strCitys = ws.getSupportCityString(strProviceName); //根据省份获得市
                DropDownList2.Items.Add("--请选择市--");
                for (int i = 0; i <strCitys.Length; i++)
                {
                    string[] strs = strCitys[i].Split(',');
                    string cityName = strs[0];
                    DropDownList2.Items.Add(cityName);
                }
            }
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedIndex > 0)
            {
                string cityName = DropDownList2.Text;
                cn.com.webxml.webservice.WeatherWS ws = new WebServiceTest.cn.com.webxml.webservice.WeatherWS();
                string[] weathers= ws.getWeather(cityName, ""); //注意getWeather的参数
                string str = "";
                //调试找出自己要获得的数据
                for (int i = 3; i < 10; i++)
                {
                    str += weathers[i] + "<br>";
                }
                str += "<img src='" + weathers[10] + "'/><img src='" + weathers[11] + "'/>";
                Label1.Text = str;
            }
        }

引用web服务如图所示:

 

根据调试获得自己需要的值:

 

 

三,自己写web服务
 (1)步骤:在web引用程序上点击右键--添加新建项--web服务
 (2)编写自己需要的方法,只需要把方法标记为[WebMethod]就可以了
 (3)具体代码:

  public class WebService1 : System.Web.Services.WebService
     {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int Add(int a, int b)
        {
            return a + b;
        }
    }

原创粉丝点击