C # WebService usage (simple weather query)

Source: Internet
Author: User

I was bored when I came back from work too early recently. Today I accidentally saw C # accessing WebService and wrote a simple weather query.

First add webreference:

Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

 

Obtain the weather service class:

 Using system; <br/> using system. collections. generic; <br/> using system. text; </P> <p> namespace weatherquery. SL <br/> {<br/> class weatherservice <br/> {<br/> # region variables <br/> private weatherwebservice. weatherwebservice serviceclient = new weatherquery. weatherwebservice. weatherwebservice (); <br/> # endregion </P> <p> # region methods <br/> // <summary> <br/> /// </Summary> <br/> // <returns> </returns> <br/> Public String [] getallservicearea () <br/>{< br/> try <br/>{< br/> return serviceclient. getsupportprovince (); <br/>}< br/> catch (exception ex) <br/>{< br/> throw ex; <br/>}</P> <p> // <summary> <br/> /// </Summary> <br/> /// <Param name = "areaname"> </param> <br/> /// <returns> </returns> <br/> Public String [] getareacitiesinarea (string areaname) <br/>{< br/> try <br/>{< br/> return serviceclient. getsupportcity (areaname); <br/>}< br/> catch (exception ex) <br/>{< br/> throw ex; <br/>}</P> <p> // <summary> <br/> /// </Summary> <br/> // <Param name = "cityname"> </param> <br/> // <returns> </returns> <br/> Public String [] getweatherbycityname (string cityname) <br/>{< br/> try <br/>{< br/> return serviceclient. getweatherbycityname (cityname); <br/>}< br/> catch (exception ex) <br/>{< br/> throw ex; <br/>}< br/> # endregion <br/>}< br/>

Bll servicehelper class:

Using system; <br/> using system. collections. generic; <br/> using system. text; </P> <p> using weatherquery. SL; </P> <p> namespace weatherquery. bll <br/> {<br/> class weatherservicehelper <br/> {<br/> # region variables <br/> private weatherservice = new weatherservice (); <br/> # endregion </P> <p> # region methods <br/> Public String [] getallsupportarea (out string errormessage) <br/>{< br/> try <Br/>{< br/> errormessage = string. empty; <br/> return weatherservice. getallservicearea (); <br/>}< br/> catch (exception ex) <br/>{< br/> errormessage = ex. message; <br/> return NULL; <br/>}</P> <p> Public String [] getallcitiesbyarea (string areaname, out string errormessage) <br/>{< br/> If (string. isnullorempty (areaname) <br/>{< br/> errormessage = "can not query null area! "; <Br/> return NULL; <br/>}< br/> else <br/> {<br/> try <br/> {<br/> errormessage = string. empty; <br/> return weatherservice. getareacitiesinarea (areaname); <br/>}< br/> catch (exception ex) <br/>{< br/> errormessage = ex. message; <br/> return NULL; <br/>}</P> <p> Public String [] getweathermessageofcity (string cityname, out string errormessage) <br/>{< br/> If (string. isnullo Rempty (cityname) <br/>{< br/> errormessage = "can not query null city! "; <Br/> return NULL; <br/>}< br/> else <br/> {<br/> try <br/> {<br/> errormessage = string. empty; <br/> // cityname = "Beijing (XXXX)" <br/> string cname = cityname. split (New char [] {'('}) [0]; <br/> return weatherservice. getweatherbycityname (cname); <br/>}< br/> catch (exception ex) <br/>{< br/> errormessage = ex. message; <br/> return NULL; <br/>}< br/> # endregion <br/>}< br/> 

UI Layer (winform 2.0)

Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; </P> <p> namespace weatherquery <br/> {<br/> Public partial class mainfrom: Form <br/>{< br/> Public mainfrom () <br/>{< br/> initializecomponent (); <br/>}</P> <p> private void btnok_click (Object sender, eventargs E) <br/>{< br/> string errormessage; <br/> string [] weathermessage = weatherservicehelper. getweathermessageofcity (cboxcity. text, out errormessage); <br/> If (weathermessage = NULL) <br/>{< br/> MessageBox. show (errormessage); <br/>}< br/> else <br/>{< br/> lboxweathermessage. datasource = weathermessage; <br/>}</P> <p> private void btncanel_click (Object sender, eventargs E) <br/>{< br/> initarea (); <br/>}</P> <p> private void mainfrom_load (Object sender, eventargs E) <br/>{< br/> initarea (); <br/>}</P> <p> private void cboxarea_selectedindexchanged (Object sender, eventargs E) <br/>{< br/> string errormessage; <br/> string [] cities = weatherservicehelper. getallcitiesbyarea (cboxarea. text, out errormessage); <br/> If (cities = NULL) <br/>{< br/> MessageBox. show (errormessage); <br/>}< br/> else <br/>{< br/> cboxcity. datasource = cities; <br/>}</P> <p> private void initarea () <br/>{< br/> string errormessage; <br/> string [] arealist = weatherservicehelper. getallsupportarea (Out errormessage); <br/> If (arealist = NULL) <br/>{< br/> MessageBox. show (errormessage); <br/>}< br/> else <br/>{< br/> cboxarea. datasource = arealist; <br/> lboxweathermessage. datasource = NULL; <br/>}< br/>

Namespace weatherquery <br/>{< br/> partial class mainfrom <br/>{< br/> /// <summary> <br/> // required designer variable. <Br/> // </Summary> <br/> private system. componentmodel. icontainer components = NULL; </P> <p> // <summary> <br/> // clear all resources in use. <Br/> /// </Summary> <br/> /// <Param name = "disposing"> true if managed resources should be released; otherwise, false. </Param> <br/> protected override void dispose (bool disposing) <br/>{< br/> If (disposing & (components! = NULL) <br/>{< br/> components. dispose (); <br/>}< br/> base. dispose (disposing ); <br/>}</P> <p> # code generated by region windows Form Designer </P> <p> // <summary> <br/> // /the designer supports the required methods-do not <br/> // use the code editor to modify the content of this method. <Br/> // </Summary> <br/> private void initializecomponent () <br/>{< br/> This. cboxarea = new system. windows. forms. comboBox (); <br/> This. cboxcity = new system. windows. forms. comboBox (); <br/> This. btnok = new system. windows. forms. button (); <br/> This. btncanel = new system. windows. forms. button (); <br/> This. lboxweathermessage = new system. windows. forms. listBox (); <br/> This. suspendlayout (); <br/> // cboxarea <br/> // <br/> This. cboxarea. formattingenabled = true; <br/> This. cboxarea. location = new system. drawing. point (28, 70); <br/> This. cboxarea. name = "cboxarea"; <br/> This. cboxarea. size = new system. drawing. size (165, 20); <br/> This. cboxarea. tabindex = 0; <br/> This. cboxarea. selectedindexchanged + = new system. eventhandler (this. cboxarea_selectedindexchanged); <br/> // cboxcity <br/> // <br/> This. cboxcity. formattingenabled = true; <br/> This. cboxcity. location = new system. drawing. point (291, 70); <br/> This. cboxcity. name = "cboxcity"; <br/> This. cboxcity. size = new system. drawing. size (169, 20); <br/> This. cboxcity. tabindex = 1; <br/> // btnok <br/> // <br/> This. btnok. location = new system. drawing. time (118,459); <br/> This. btnok. name = "btnok"; <br/> This. btnok. size = new system. drawing. size (75, 23); <br/> This. btnok. tabindex = 3; <br/> This. btnok. TEXT = "query"; <br/> This. btnok. usevisualstylebackcolor = true; <br/> This. btnok. click + = new system. eventhandler (this. btnok_click); <br/> // btncanel <br/> // <br/> This. btncanel. location = new system. drawing. time (291,459); <br/> This. btncanel. name = "btncanel"; <br/> This. btncanel. size = new system. drawing. size (75, 23); <br/> This. btncanel. tabindex = 4; <br/> This. btncanel. TEXT = "reselect"; <br/> This. btncanel. usevisualstylebackcolor = true; <br/> This. btncanel. click + = new system. eventhandler (this. btncanel_click); <br/> // lboxweathermessage <br/> // <br/> This. lboxweathermessage. formattingenabled = true; <br/> This. lboxweathermessage. horizontalscrollbar = true; <br/> This. lboxweathermessage. itemheight = 12; <br/> This. lboxweathermessage. location = new system. drawing. time (28,130); <br/> This. lboxweathermessage. name = "lboxweathermessage"; <br/> This. lboxweathermessage. scrollalwaysvisible = true; <br/> This. lboxweathermessage. size = new system. drawing. size (544,316); <br/> This. lboxweathermessage. tabindex = 5; <br/> // mainfrom <br/> // <br/> This. autoscaledimensions = new system. drawing. sizef (6f, 12f); <br/> This. autoscalemode = system. windows. forms. autoscalemode. font; <br/> This. clientsize = new system. drawing. size (607,494); <br/> This. controls. add (this. lboxweathermessage); <br/> This. controls. add (this. btncanel); <br/> This. controls. add (this. btnok); <br/> This. controls. add (this. cboxcity); <br/> This. controls. add (this. cboxarea); <br/> This. name = "mainfrom"; <br/> This. TEXT = "simple weather query"; <br/> This. load + = new system. eventhandler (this. mainfrom_load); <br/> This. resumelayout (false); </P> <p >}</P> <p ># endregion </P> <p> private system. windows. forms. comboBox cboxarea; <br/> private system. windows. forms. comboBox cboxcity; <br/> private system. windows. forms. button btnok; <br/> private system. windows. forms. button btncanel; <br/> private system. windows. forms. listBox lboxweathermessage; <br/> private weatherquery. bll. weatherservicehelper = new weatherquery. bll. weatherservicehelper (); <br/>}</P> <p>

 

It's really boring.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.