1.新建一个网站或者web应用程序,添加一个aspx页面,用于展示天气数据。(这个应该不用细讲吧)
2.在网上找一个免费的天气预报的接口,我用的是Webxml网站的,地址如下:
3.在项目目录下, 引用 — 添加服务引用,弹出对话框,然后输入接口地址,点击前往,命名空间可以改成你想要的,如下图:
一般处理程序代码如下:
using System.Web;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text; namespace WeatherTest.ashx{ ////// weatherHandler 的摘要说明 /// public class weatherHandler : IHttpHandler { WeatherWsClient.WeatherWSSoapClient client = new WeatherWsClient.WeatherWSSoapClient(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string[] result = null; string option = 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; } string str = ConvertToString(result, option); context.Response.Write(str); } ////// 数组转字符串 /// /// /// ///private string ConvertToString(string[] result, string option) { StringBuilder sb = new StringBuilder(); foreach (string item in result) { sb.Append(item+"|"); } return sb.ToString(); } /// /// 省份 /// ///private string[] GetProvinces() { return client.getRegionProvince(); } /// /// 城市 /// /// ///private string[] GetCitys(string provinceid) { return client.getSupportCityString(provinceid); } /// /// 天气数据 /// /// /// ///private string[] GetWeather(string cityid, string userid) { return client.getWeather(cityid, userid); } public bool IsReusable { get { return false; } } }}