微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService---web服务的使用

一,什么是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;         }     }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐