1. Create a new Web site or Web application and add an ASPX page to display the weather data. (This should not be talked about in detail)
2. Find an interface for free weather forecast on the Internet, I use the Webxml website, the address is as follows:
Http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
3. In the project directory, reference-add Service Reference, popup dialog, then enter the interface address, click Go, the namespace can be changed to you want, such as:
The general handler code is as follows:
usingsystem.web;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Text;namespaceweathertest.ashx{/// <summary> ///Summary description of Weatherhandler/// </summary> Public classWeatherhandler:ihttphandler {weatherwsclient.weatherwssoapclient client=Newweatherwsclient.weatherwssoapclient (); Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; string[] result =NULL; stringOption = Context. request.form["option"]; Switch(option) { Case "Province": Result=getprovinces (); Break; Case " City": Result= Getcitys (context. request.form["Provinceid"]); Break; Case "Weather": Result= GetWeather (context. request.form["Cityid"],NULL); Break; } stringstr =converttostring (result, option); Context. Response.Write (str); } /// <summary> ///Array to string/// </summary> /// <param name= "result" ></param> /// <param name= "option" ></param> /// <returns></returns> Private stringConvertToString (string[] result,stringoption) {StringBuilder SB=NewStringBuilder (); foreach(stringIteminchresult) {sb. Append (Item+"|"); } returnsb. ToString (); } /// <summary> ///Provinces/// </summary> /// <returns></returns> Private string[] getprovinces () {returnclient.getregionprovince (); } /// <summary> ///City/// </summary> /// <param name= "Provinceid" ></param> /// <returns></returns> Private string[] Getcitys (stringProvinceid) { returnclient.getsupportcitystring (Provinceid); } /// <summary> ///Weather Data/// </summary> /// <param name= "Cityid" ></param> /// <param name= "userid" ></param> /// <returns></returns> Private string[] GetWeather (stringCityid,stringuserid) { returnClient.getweather (Cityid, UserID); } Public BOOLisreusable {Get { return false; } } }}View Code
. NET request WebService simple implementation of the weather forecast function