[Asp.Net]ASP.Net获得新浪天气预报几种方式总结

刚赶工写了一个获取新浪天气预报的功能,顺便把代码分享了出来,以后再有这样的工作,大家可以直接把代码拿来使用。

1.利用新浪提供给的iframe直接嵌入,这种方式非常的简单,但是却没有交互性。代码如下:

<iframe frameborder="0" src="http://php.weather.sina.com.cn/widget/weather.php" scrolling="no" width="246" height="360"></iframe>

 

2.抓取当天的天气,以指定格式输出。

涉及的核心代码如下:


public static ArrayList GetWeather(string code)
        {
            
/*
            [0] "北京 "string
            [1] "雷阵雨 "string
            [2] "9℃" string
            [3] "29℃"string
            [4] "小于3级"string
            
*/
            
string html = "";
            
try
            {
                HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create("http://weather.sina.com.cn/iframe/weather/" + code + "_w.html ");
                request.Method 
= "Get";
                
//request.Timeout   =   1;
                request.ContentType = "application/x-www-form-urlencoded ";
                WebResponse response 
= request.GetResponse();
                Stream s 
= response.GetResponseStream();
                StreamReader sr 
= new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
                html 
= sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            
catch (Exception err)
            {
                
throw new Exception("访问地址出错~~~ ");
            }
            
int count = html.Length;
            
int starIndex = html.IndexOf("<table "0, count);
            
int endIndex = html.IndexOf("</table>", starIndex, count  starIndex);
            html 
= html.Substring(starIndex, endIndex  starIndex + 8);
            
//得到城市
            int cityStartIndex = html.IndexOf("<b>"0, html.Length);
            
int cityEndIndex = html.IndexOf("</b>"0, html.Length);
            
string City = html.Substring(cityStartIndex + 3, cityEndIndex  cityStartIndex  3);
            
//得到天气
            int weatherStartIndex = html.IndexOf("<b>", cityEndIndex);
            
int weatherEndIndex = html.IndexOf("</b>", weatherStartIndex);
            
string Weather = html.Substring(weatherStartIndex + 3, weatherEndIndex  weatherStartIndex  3);
            
//得到温度

            
int temperatureStartIndex = html.IndexOf("<b", weatherEndIndex);
            
int temperatureEndIndex = html.IndexOf("</b>", weatherEndIndex + 3);
            
string Temperature = html.Substring(temperatureStartIndex + 21, temperatureEndIndex  temperatureStartIndex  21);
            
int int1 = Temperature.IndexOf(""0);
            
int int2 = Temperature.IndexOf(""0);
            
int int3 = Temperature.IndexOf("", int2);
            
string MinTemperature = Temperature.Substring(int2 + 1, int3  int2);
            
string MaxTemperature = Temperature.Substring(0, int2  int1 + 2);
            
//得到风力
            int windforceStartIndex = html.IndexOf("风力:", temperatureEndIndex);
            
int windforceEndIndex = html.IndexOf("<br>", windforceStartIndex);
            
string Windforce = html.Substring(windforceStartIndex + 3, windforceEndIndex  windforceStartIndex  3);
            
if (Windforce.Contains("小于"&& (!Windforce.Contains("等于")))                  //判断风力是否含有"小于"或"小于等于"字样将,如果有的话,将其替换为2-
            {
                
//Windforce = Windforce.Replace("小于", "2-");
                string strWindforce = Windforce.Substring(2, Windforce.Length  3);
                
int minWindforce = Int32.Parse(strWindforce)  1;
                Windforce 
= Windforce.Replace("小于", minWindforce.ToString() + "");
            }
            
else if (Windforce.Contains("小于等于"))
            {
                
string strWindforce = Windforce.Substring(4, Windforce.Length  5);
                
int minWindforce = Int32.Parse(strWindforce)  1;
                Windforce 
= Windforce.Replace("小于等于", minWindforce.ToString() + "");
            }
            ArrayList al 
= new ArrayList();
            al.Add(City);
            al.Add(Weather);
            al.Add(MinTemperature);
            al.Add(MaxTemperature);
            al.Add(Windforce);
            
return al;
        }

 这里涉及到一个ConvertCode类,它的作用是用于把城市转换为对应的全国统一的编码,代码如下:


using System;
using System.Collections.Generic;
using System.Web;
namespace WebApplication8
{
    
public class ConvertCode
    {
        
public static string ConvertCodeByCity(string City)
        {
            
string Code = "";
            
switch (City)
            {
                
case "北京":
                    Code 
= "110100";
                    
break;
                
case "天津":
                    Code 
= "120100";
                    
break;
                
case "石家庄":
                    Code 
= "130101";
                    
break;
                
case "唐山":
                    Code 
= "130201";
                    
break;
                
case "秦皇岛":
                    Code 
= "130301";
                    
break;
                
case "张家口":
                    Code 
= "130701";
                    
break;
                
case "承德":
                    Code 
= "130801";
                    
break;
                
case "廊坊":
                    Code 
= "131001";
                    
break;
                
case "邯郸":
                    Code 
= "130401";
                    
break;
                
case "邢台":
                    Code 
= "130501";
                    
break;
                
case "保定":
                    Code 
= "130601";
                    
break;
                
case "沧州":
                    Code 
= "130901";
                    
break;
                
case "衡水":
                    Code 
= "133001";
                    
break;
                
case "太原":
                    Code 
= "140101";
                    
break;
                
case "大同":
                    Code 
= "140201";
                    
break;
                
case "阳泉":
                    Code 
= "140301";
                    
break;
                
case "晋城":
                    Code 
= "140501";
                    
break;
                
case "朔州":
                    Code 
= "140601";
                    
break;
                
case "忻州":
                    Code 
= "142201";
                    
break;
                
case "离石":
                    Code 
= "142331";
                    
break;
                
case "榆次":
                    Code 
= "142401";
                    
break;
                
case "临汾":
                    Code 
= "142601";
                    
break;
                
case "运城":
                    Code 
= "142701";
                    
break;
                
case "长治":
                    Code 
= "140401";
                    
break;
                
case "呼和浩特":
                    Code 
= "150101";
                    
break;
                
case "包头":
                    Code 
= "150201";
                    
break;
                
case "乌海":
                    Code 
= "150301";
                    
break;
                
case "集宁":
                    Code 
= "152601";
                    
break;
                
case "巴彦浩特":                           //这个城市搜索不到
                    Code = "152701";
                    
break;
                
case "临河":
                    Code 
= "152801";
                    
break;
                
case "鄂尔多斯":                           //这个城市搜索不到
                    Code = "152921";
                    
break;
                
case "赤峰":
                    Code 
= "150401";
                    
break;
                
case "通辽":
                    Code 
= "152301";
                    
break;
                
case "锡林浩特":
                    Code 
= "152502";
                    
break;
                
case "海拉尔":                            //这个城市搜索不到
                    Code = "152101";
                    
break;
                
case "乌兰浩特":
                    Code 
= "152201";
                    
break;
                
case "沈阳":
                    Code 
= "210101";
                    
break;
                
case "大连":
                    Code 
= "210201";
                    
break;
                
case "鞍山":
                    Code 
= "210301";
                    
break;
                
case "抚顺":
                    Code 
= "210401";
                    
break;
                
case "本溪":
                    Code 
= "210501";
                    
break;
                
case "锦州":
                    Code 
= "210701";
                    
break;
                
case "营口":
                    Code 
= "210801";
                    
break;
                
case "阜新":
                    Code 
= "210901";
                    
break;
                
case "盘锦":
                    Code 
= "211101";
                    
break;
                
case "铁岭":
                    Code 
= "211201";
                    
break;
                
case "朝阳":
                    Code 
= "211301";
                    
break;
                
case "葫芦岛":
                    Code 
= "211401";
                    
break;
                
case "丹东":
                    Code 
= "210601";
                    
break;
                
case "长春":
                    Code 
= "220101";
                    
break;
                
case "吉林":
                    Code 
= "220201";
                    
break;
                
case "四平":
                    Code 
= "220301";
                    
break;
                
case "辽源":
                    Code 
= "220401";
                    
break;
                
case "松原":
                    Code 
= "220601";
                    
break;
                
case "白城":
                    Code 
= "222301";
                    
break;
                
case "延边":
                    Code 
= "222401";
                    
break;
                
case "通化":
                    Code 
= "220501";
                    
break;
                
case "哈尔滨":
                    Code 
= "230101";
                    
break;
                
case "鸡西":
                    Code 
= "230301";
                    
break;
                
case "鹤岗":
                    Code 
= "230401";
                    
break;
                
case "双鸭山":
                    Code 
= "230501";
                    
break;
                
case "伊春":
                    Code 
= "230701";
                    
break;
                
case "佳木斯":
                    Code 
= "230801";
                    
break;
                
case "七台河":
                    Code 
= "230901";
                    
break;
                
case "牡丹江":
                    Code 
= "231001";
                    
break;
                
case "绥化":
                    Code 
= "232301";
                    
break;
                
case "齐齐哈尔":
                    Code 
= "230201";
                    
break;
                
case "大庆":
                    Code 
= "230601";
                    
break;
                
case "黑河":
                    Code 
= "232601";
                    
break;
                
case "大兴安岭":
                    Code 
= "232700";
                    
break;
                
case "上海":
                    Code 
= "310100";
                    
break;
                
case "南京":
                    Code 
= "320101";
                    
break;
                
case "无锡":
                    Code 
= "320201";
                    
break;
                
case "徐州":
                    Code 
= "320301";
                    
break;
                
case "常州":
                    Code 
= "320401";
                    
break;
                
case "苏州":
                    Code 
= "320501";
                    
break;
                
case "南通":
                    Code 
= "320600";
                    
break;
                
case "连云港":
                    Code 
= "320701";
                    
break;
                
case "淮阴":
                    Code 
= "320801";
                    
break;
                
case "盐城":
                    Code 
= "320901";
                    
break;
                
case "扬州":
                    Code 
= "321001";
                    
break;
                
case "镇江":
                    Code 
= "321101";
                    
break;
                
case "泰州":
                    Code 
= "321102";
                    
break;
                
case "宿迁":
                    Code 
= "321103";
                    
break;
                
case "杭州":
                    Code 
= "330101";
                    
break;
                
case "宁波":
                    Code 
= "330201";
                    
break;
                
case "温州":
                    Code 
= "330301";
                    
break;
                
case "嘉兴":
                    Code 
= "330401";
                    
break;
                
case "湖州":
                    Code 
= "330501";
                    
break;
                
case "绍兴":
                    Code 
= "330601";
                    
break;
                
case "金华":
                    Code 
= "330701";
                    
break;
                
case "衢州":
                    Code 
= "330801";
                    
break;
                
case "舟山":
                    Code 
= "330901";
                    
break;
                
case "丽水":
                    Code 
= "332501";
                    
break;
                
case "台州":
                    Code 
= "332602";
                    
break;
                
case "合肥":
                    Code 
= "340101";
                    
break;
                
case "芜湖":
                    Code 
= "340201";
                    
break;
                
case "蚌埠":
                    Code 
= "340301";
                    
break;
                
case "淮南":
                    Code 
= "340401";
                    
break;
                
case "马鞍山":
                    Code 
= "340501";
                    
break;
                
case "淮北":
                    Code 
= "340601";
                    
break;
                
case "铜陵":
                    Code 
= "340701";
                    
break;
                
case "安庆":
                    Code 
= "340801";
                    
break;
                
case "黄山市":
                    Code 
= "341001";
                    
break;
                
case "阜阳":
                    Code 
= "342101";
                    
break;
                
case "宿州":
                    Code 
= "342201";
                    
break;
                
case "滁州":
                    Code 
= "342301";
                    
break;
                
case "六安":
                    Code 
= "342401";
                    
break;
                
case "宣城":
                    Code 
= "342501";
                    
break;
                
case "巢湖":
                    Code 
= "342601";
                    
break;
                
case "池州":
                    Code 
= "342901";
                    
break;
                
case "福州":
                    Code 
= "350101";
                    
break;
                
case "厦门":
                    Code 
= "350201";
                    
break;
                
case "莆田":
                    Code 
= "350301";
                    
break;
                
case "三明":
                    Code 
= "350401";
                    
break;
                
case "泉州":
                    Code 
= "350501";
                    
break;
                
case "漳州":
                    Code 
= "350601";
                    
break;
                
case "南平":
                    Code 
= "352101";
                    
break;
                
case "宁德":
                    Code 
= "352201";
                    
break;
                
case "龙岩":
                    Code 
= "352601";
                    
break;
                
case "陇南":
                    Code 
= "352602";
                    
break;
                
case "庆阳":
                    Code 
= "352603";
                    
break;
                
case "南昌":
                    Code 
= "360101";
                    
break;
                
case "景德镇":
                    Code 
= "360201";
                    
break;
                
case "赣州":
                    Code 
= "362101";
                    
break;
                
case "萍乡":
                    Code 
= "360301";
                    
break;
                
case "九江":
                    Code 
= "360401";
                    
break;
                
case "新余":
                    Code 
= "360501";
                    
break;
                
case "鹰潭":
                    Code 
= "360601";
                    
break;
                
case "宜春":
                    Code 
= "362201";
                    
break;
                
case "上饶":
                    Code 
= "362301";
                    
break;
                
case "吉安":
                    Code 
= "362401";
                    
break;
                
case "济南":
                    Code 
= "370101";
                    
break;
                
case "青岛":
                    Code 
= "370201";
                    
break;
                
case "淄博":
                    Code 
= "370301";
                    
break;
                
case "枣庄":
                    Code 
= "370401";
                    
break;
                
case "东营":
                    Code 
= "370501";
                    
break;
                
case "烟台":
                    Code 
= "370601";
                    
break;
                
case "潍坊":
                    Code 
= "370701";
                    
break;
                
case "济宁":
                    Code 
= "370801";
                    
break;
                
case "泰安":
                    Code 
= "370901";
                    
break;
                
case "威海":
                    Code 
= "371001";
                    
break;
                
case "日照":
                    Code 
= "371100";
                    
break;
                
case "滨州":
                    Code 
= "372301";
                    
break;
                
case "德州":
                    Code 
= "372401";
                    
break;
                
case "聊城":
                    Code 
= "372501";
                    
break;
                
case "临沂":
                    Code 
= "372801";
                    
break;
                
case "菏泽":
                    Code 
= "372901";
                    
break;
                
case "莱芜":
                    Code 
= "372902";
                    
break;
                
case "郑州":
                    Code 
= "410101";
                    
break;
                
case "开封":
                    Code 
= "410201";
                    
break;
                
case "洛阳":
                    Code 
= "410301";
                    
break;
                
case "平顶山":
                    Code 
= "410401";
                    
break;
                
case "安阳":
                    Code 
= "410501";
                    
break;
                
case "鹤壁":
                    Code 
= "410601";
                    
break;
                
case "新乡":
                    Code 
= "410701";
                    
break;
                
case "焦作":
                    Code 
= "410801";
                    
break;
                
case "濮阳":
                    Code 
= "410901";
                    
break;
                
case "许昌":
                    Code 
= "411001";
                    
break;
                
case "漯河":
                    Code 
= "411101";
                    
break;
                
case "三门峡":
                    Code 
= "411201";
                    
break;
                
case "商丘":
                    Code 
= "412301";
                    
break;
                
case "周口":
                    Code 
= "412701";
                    
break;
                
case "驻马店":
                    Code 
= "412801";
                    
break;
                
case "南阳":
                    Code 
= "412901";
                    
break;
                
case "信阳":
                    Code 
= "413001";
                    
break;
                
case "武汉":
                    Code 
= "420101";
                    
break;
                
case "黄石":
                    Code 
= "420201";
                    
break;
                
case "十堰":
                    Code 
= "420301";
                    
break;
                
case "随州":
                    Code 
= "420400";
                    
break;
                
case "宜昌":
                    Code 
= "420501";
                    
break;
                
case "襄樊":
                    Code 
= "420601";
                    
break;
                
case "鄂州":
                    Code 
= "420701";
                    
break;
                
case "荆门":
                    Code 
= "420801";
                    
break;
                
case "黄冈":
                    Code 
= "422103";
                    
break;
                
case "孝感":
                    Code 
= "422201";
                    
break;
                
case "咸宁":
                    Code 
= "422301";
                    
break;
                
case "荆州":
                    Code 
= "422421";
                    
break;
                
case "恩施":
                    Code 
= "422801";
                    
break;
                
case "长沙":
                    Code 
= "430101";
                    
break;
                
case "衡阳":
                    Code 
= "430401";
                    
break;
                
case "邵阳":
                    Code 
= "430501";
                    
break;
                
case "郴州":
                    Code 
= "432801";
                    
break;
                
case "永州":
                    Code 
= "432901";
                    
break;
                
case "韶山":
                    Code 
= "430801";
                    
break;
                
case "张家界":
                    Code 
= "430802";
                    
break;
                
case "怀化":
                    Code 
= "433001";
                    
break;
                
case "吉首":
                    Code 
= "433101";
                    
break;
                
case "株洲":
                    Code 
= "430201";
                    
break;
                
case "湘潭":
                    Code 
= "430301";
                    
break;
                
case "岳阳":
                    Code 
= "430601";
                    
break;
                
case "常德":
                    Code 
= "430701";
                    
break;
                
case "益阳":
                    Code 
= "432301";
                    
break;
                
case "娄底":
                    Code 
= "432501";
                    
break;
                
case "广州":
                    Code 
= "440101";
                    
break;
                
case "佛山":
                    Code 
= "440601";
                    
break;
                
case "深圳":
                    Code 
= "440301";
                    
break;
                
case "汕尾":
                    Code 
= "441501";
                    
break;
                
case "惠州":
                    Code 
= "441301";
                    
break;
                
case "河源":
                    Code 
= "441601";
                    
break;
                
case "清远":
                    Code 
= "441801";
                    
break;
                
case "东莞":
                    Code 
= "441901";
                    
break;
                
case "珠海":
                    Code 
= "440401";
                    
break;
                
case "江门":
                    Code 
= "440701";
                    
break;
                
case "肇庆":
                    Code 
= "441201";
                    
break;
                
case "中山":
                    Code 
= "442001";
                    
break;
                
case "湛江":
                    Code 
= "440801";
                    
break;
                
case "茂名":
                    Code 
= "440901";
                    
break;
                
case "韶关":
                    Code 
= "440201";
                    
break;
                
case "汕头":
                    Code 
= "440501";
                    
break;
                
case "梅州":
                    Code 
= "441401";
                    
break;
                
case "阳江":
                    Code 
= "441701";
                    
break;
                
case "潮州":
                    Code 
= "441702";
                    
break;
                
case "顺德":
                    Code 
= "441703";
                    
break;
                
case "揭阳":
                    Code 
= "441704";
                    
break;
                
case "云浮":
                    Code 
= "441705";
                    
break;
                
case "南宁":
                    Code 
= "450101";
                    
break;
                
case "梧州":
                    Code 
= "450401";
                    
break;
                
case "玉林":
                    Code 
= "452501";
                    
break;
                
case "桂林":
                    Code 
= "450301";
                    
break;
                
case "百色":
                    Code 
= "452601";
                    
break;
                
case "河池":
                    Code 
= "452701";
                    
break;
                
case "钦州":
                    Code 
= "452802";
                    
break;
                
case "柳州":
                    Code 
= "450201";
                    
break;
                
case "北海":
                    Code 
= "450501";
                    
break;
                
case "防城港":
                    Code 
= "450502";
                    
break;
                
case "贵港":
                    Code 
= "450503";
                    
break;
                
case "贺州":
                    Code 
= "450504";
                    
break;
                
case "海口":
                    Code 
= "460100";
                    
break;
                
case "三亚":
                    Code 
= "460200";
                    
break;
                
case "西沙群岛":
                    Code 
= "460300";
                    
break;
                
case "成都":
                    Code 
= "510101";
                    
break;
                
case "眉山":
                    Code 
= "513321";
                    
break;
                
case "雅安":
                    Code 
= "513101";
                    
break;
                
case "峨嵋山":
                    Code 
= "513229";
                    
break;
                
case "自贡":
                    Code 
= "510301";
                    
break;
                
case "重庆":
                    Code 
= "500100";
                    
break;
                
case "万州":
                    Code 
= "500102";
                    
break;
                
case "涪陵":
                    Code 
= "500103";
                    
break;
                
case "南充":
                    Code 
= "512901";
                    
break;
                
case "泸州":
                    Code 
= "510501";
                    
break;
                
case "德阳":
                    Code 
= "510601";
                    
break;
                
case "绵阳":
                    Code 
= "510701";
                    
break;
                
case "遂宁":
                    Code 
= "510901";
                    
break;
                
case "内江":
                    Code 
= "511001";
                    
break;
                
case "乐山":
                    Code 
= "511101";
                    
break;
                
case "宜宾":
                    Code 
= "512501";
                    
break;
                
case "广元":
                    Code 
= "510801";
                    
break;
                
case "达州":
                    Code 
= "513021";
                    
break;
                
case "资阳":
                    Code 
= "513401";
                    
break;
                
case "攀枝花":
                    Code 
= "510401";
                    
break;
                
case "阿坝":
                    Code 
= "510402";
                    
break;
                
case "甘孜":
                    Code 
= "510403";
                    
break;
                
case "凉山":
                    Code 
= "510404";
                    
break;
                
case "广安":
                    Code 
= "510405";
                    
break;
                
case "巴中":
                    Code 
= "510406";
                    
break;
                
case "黔江":
                    Code 
= "500239";
                    
break;
                
case "贵阳":
                    Code 
= "520101";
                    
break;
                
case "六盘水":
                    Code 
= "520200";
                    
break;
                
case "铜仁":
                    Code 
= "522201";
                    
break;
                
case "安顺":
                    Code 
= "522501";
                    
break;
                
case "凯里":
                    Code 
= "522601";
                    
break;
                
case "都匀":
                    Code 
= "522701";
                    
break;
                
case "兴义":
                    Code 
= "522301";
                    
break;
                
case "毕节":
                    Code 
= "522421";
                    
break;
                
case "遵义":
                    Code 
= "522101";
                    
break;
                
case "昆明":
                    Code 
= "530101";
                    
break;
                
case "德宏":
                    Code 
= "530201";
                    
break;
                
case "曲靖":
                    Code 
= "532201";
                    
break;
                
case "楚雄":
                    Code 
= "532301";
                    
break;
                
case "玉溪":
                    Code 
= "532401";
                    
break;
                
case "红河":
                    Code 
= "532501";
                    
break;
                
case "文山":
                    Code 
= "532621";
                    
break;
                
case "思茅":
                    Code 
= "532721";
                    
break;
                
case "昭通":
                    Code 
= "532101";
                    
break;
                
case "西双版纳":
                    Code 
= "532821";
                    
break;
                
case "大理":
                    Code 
= "532901";
                    
break;
                
case "保山":
                    Code 
= "533001";
                    
break;
                
case "怒江":
                    Code 
= "533121";
                    
break;
                
case "丽江":
                    Code 
= "533221";
                    
break;
                
case "迪庆":
                    Code 
= "533321";
                    
break;
                
case "临沧":
                    Code 
= "533521";
                    
break;
                
case "拉萨":
                    Code 
= "540101";
                    
break;
                
case "昌都":
                    Code 
= "542121";
                    
break;
                
case "山南":
                    Code 
= "542221";
                    
break;
                
case "日喀则":
                    Code 
= "542301";
                    
break;
                
case "那曲":
                    Code 
= "542421";
                    
break;
                
case "阿里":
                    Code 
= "542523";
                    
break;
                
case "林芝":
                    Code 
= "542621";
                    
break;
                
case "西安":
                    Code 
= "610101";
                    
break;
                
case "铜川":
                    Code 
= "610201";
                    
break;
                
case "宝鸡":
                    Code 
= "610301";
                    
break;
                
case "咸阳":
                    Code 
= "610401";
                    
break;
                
case "渭南":
                    Code 
= "612101";
                    
break;
                
case "汉中":
                    Code 
= "612301";
                    
break;
                
case "安康":
                    Code 
= "612401";
                    
break;
                
case "商洛":
                    Code 
= "612501";
                    
break;
                
case "延安":
                    Code 
= "612601";
                    
break;
                
case "榆林":
                    Code 
= "612701";
                    
break;
                
case "兰州":
                    Code 
= "620101";
                    
break;
                
case "白银":
                    Code 
= "620401";
                    
break;
                
case "金昌":
                    Code 
= "620301";
                    
break;
                
case "天水":
                    Code 
= "620501";
                    
break;
                
case "张掖":
                    Code 
= "622201";
                    
break;
                
case "武威":
                    Code 
= "622301";
                    
break;
                
case "定西":
                    Code 
= "622421";
                    
break;
                
case "平凉":
                    Code 
= "622701";
                    
break;
                
case "临夏":
                    Code 
= "622901";
                    
break;
                
case "嘉峪关":
                    Code 
= "620201";
                    
break;
                
case "酒泉":
                    Code 
= "622102";
                    
break;
                
case "西宁":
                    Code 
= "630100";
                    
break;
                
case "果洛":
                    Code 
= "632121";
                    
break;
                
case "海西":
                    Code 
= "632221";
                    
break;
                
case "格尔木":
                    Code 
= "632321";
                    
break;
                
case "海东":
                    Code 
= "632521";
                    
break;
                
case "海北":
                    Code 
= "632621";
                    
break;
                
case "玉树":
                    Code 
= "632721";
                    
break;
                
case "黄南":
                    Code 
= "632802";
                    
break;
                
case "银川":
                    Code 
= "640101";
                    
break;
                
case "石嘴山":
                    Code 
= "640201";
                    
break;
                
case "吴忠":
                    Code 
= "642101";
                    
break;
                
case "固原":
                    Code 
= "642221";
                    
break;
                
case "乌鲁木齐":
                    Code 
= "650101";
                    
break;
                
case "克拉玛依":
                    Code 
= "650201";
                    
break;
                
case "吐鲁番":
                    Code 
= "652101";
                    
break;
                
case "哈密":
                    Code 
= "652201";
                    
break;
                
case "昌吉":
                    Code 
= "652301";
                    
break;
                
case "博乐":
                    Code 
= "652701";
                    
break;
                
case "库尔勒":
                    Code 
= "652801";
                    
break;
                
case "阿克苏":
                    Code 
= "652901";
                    
break;
                
case "克州":
                    Code 
= "653001";
                    
break;
                
case "喀什":
                    Code 
= "653101";
                    
break;
                
case "伊犁":
                    Code 
= "654101";
                    
break;
                
case "石河子":
                    Code 
= "655001";
                    
break;
                
case "塔城":
                    Code 
= "655002";
                    
break;
                
case "阿勒泰":
                    Code 
= "655003";
                    
break;
                
case "台北":
                    Code 
= "710001";
                    
break;
                
case "辽阳":
                    Code 
= "211001";
                    
break;
                
case "和田":
                    Code 
= "653201";
                    
break;
                
case "澳门":
                    Code 
= "820000";
                    
break;
                
case "香港":
                    Code 
= "810000";
                    
break;
                    
                
default:
                    
break;
            }
            
return Code;
        }
    }
}

3.获取三天以内的天气,以指定格式输出。

核心代码如下:


public static ArrayList GetThreeDayWeather(string City)
        {
            ArrayList al 
= new ArrayList();
            
/*
           [0] "今天 北京"              string
           [1] "2009-04-17,星期五"     string
           [2] "晴转多云"               string
           [3] "12℃"                   string
           [4] "25℃"                   string
           [5] "2-3级"                  string
           [6] "明天 北京"              string
           [7] "2009-04-18,星期六"     string
           [8] "阴转阵雨"               string
           [9] "11℃"                   string
           [10] "21℃"                  string
           [11] "2-3级"                 string
           [12] "后天 北京"             string
           [13] "2009-04-19,星期日"    string
           [14] "多云转阵雨"            string
           [15] "9℃"                   string
           [16] "20℃"                  string
           [17] "2-3级"                 string         
           
*/
            
string Html = "";       //返回来的网页的源码
            ASCIIEncoding encoding = new ASCIIEncoding();
            
string postData = string.Format("city=" + City);
            
byte[] data = encoding.GetBytes(postData);
            
try
            {
                HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + System.Web.HttpContext.Current.Server.UrlEncode(City) + "&f=1&dpc=1");
                request.Method 
= "Get";
                request.ContentType 
= "application/x-www-form-urlencoded ";
                WebResponse response 
= request.GetResponse();
                Stream s 
= response.GetResponseStream();
                StreamReader sr 
= new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
                Html 
= sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            
catch (Exception err)
            {
                
throw new Exception("访问地址出错~~~ ");
            }
            
//去除多余代码,便于分析跟提高效率
            int count = Html.Length;
            
int starIndex = Html.IndexOf("<div id=\"Weather3DBlk\">"0, count);
            
int endIndex = Html.IndexOf("<div id=\"SideBar\">"0);
            Html 
= Html.Substring(starIndex, endIndex  starIndex);
            
try
            {
                
#region 得到今天的天气
                
//得到今天的标识跟城市
                int firstDayAndCityStartIndex = Html.IndexOf("<h3>"0);
                
int firstDayAndCityEndIndex = Html.IndexOf("</h3>"0);
                
string FirstDayAndCity = Html.Substring(firstDayAndCityStartIndex + 4, firstDayAndCityEndIndex  firstDayAndCityStartIndex  4);
                
//得到今天的日期跟星期
                int firstDateStartIndex = Html.IndexOf("<p>", firstDayAndCityEndIndex);
                
int firstDateEndIndex = Html.IndexOf("</p>", firstDayAndCityEndIndex);
                
string FirstDate = Html.Substring(firstDateStartIndex + 3, firstDateEndIndex  firstDateStartIndex  3).Replace("&nbsp;""");
                
//得到今天的天气
                int firstWeatherStartIndex = Html.IndexOf("<div class=\"Weather_TP\">", firstDateEndIndex);
                
int firstWeatherEndIndex = Html.IndexOf(" ", firstWeatherStartIndex + 24);
                
string FirstWeather = Html.Substring(firstWeatherStartIndex + 24, firstWeatherEndIndex  firstWeatherStartIndex  24);
                
//得到今天的温度

                
int firstTemperatureStartIndex = firstWeatherEndIndex + 1;
                
int firstTemperatureEndIndex = Html.IndexOf("</div>", firstTemperatureStartIndex);
                
string FirstTemperature = Html.Substring(firstTemperatureStartIndex, firstTemperatureEndIndex  firstTemperatureStartIndex);
                
int int1 = FirstTemperature.IndexOf(""0);
                
int int2 = FirstTemperature.IndexOf(""0);
                
int int3 = FirstTemperature.IndexOf("", int2);
                
string FirstMinTemperature = FirstTemperature.Substring(int2 + 1, int3  int2);
                
string FirstMaxTemperature = FirstTemperature.Substring(0, int2  int1 + 2);
                
//得到今天的风力
                int firstWindforceStartIndex = Html.IndexOf("风力:", firstTemperatureEndIndex);
                
int firstWindforceEndIndex = Html.IndexOf("</div>", firstWindforceStartIndex);
                
string FirstWindforce = Html.Substring(firstWindforceStartIndex + 3, firstWindforceEndIndex  firstWindforceStartIndex  3);
                
if (FirstWindforce.Contains(""))
                {
                }
                
else if (FirstWindforce.Contains("<"))                  //判断风力是否含有"<"或"≤"字样将,如果有的话,将其替换为2-
                {
                    
string strWindforce = FirstWindforce.Substring(1, FirstWindforce.Length  2);
                    
int minWindforce = Int32.Parse(strWindforce)  1;
                    FirstWindforce 
= FirstWindforce.Replace("<", minWindforce.ToString() + "");
                }
                
else if (FirstWindforce.Contains(""))
                {
                    
string strWindforce = FirstWindforce.Substring(1, FirstWindforce.Length  2);
                    
int minWindforce = Int32.Parse(strWindforce)  1;
                    FirstWindforce 
= FirstWindforce.Replace("", minWindforce.ToString() + "");
                }
                
#endregion
                
#region 得到明天的天气
                
//得到明天的标识跟城市
                int secondDayAndCityStartIndex = Html.IndexOf("<h3>", firstWindforceEndIndex);
                
int secondDayAndCityEndIndex = Html.IndexOf("</h3>", secondDayAndCityStartIndex);
                
string secondDayAndCity = Html.Substring(secondDayAndCityStartIndex + 4, secondDayAndCityEndIndex  secondDayAndCityStartIndex  4);
                
//得到明天的日期跟星期
                int secondDateStartIndex = Html.IndexOf("<p>", secondDayAndCityEndIndex);
                
int secondDateEndIndex = Html.IndexOf("</p>", secondDateStartIndex);
                
string SecondDate = Html.Substring(secondDateStartIndex + 3, secondDateEndIndex  secondDateStartIndex  3).Replace("&nbsp;""");
                
//得到明天的天气
                int secondWeatherStartIndex = Html.IndexOf("<div class=\"Weather_TP\">", secondDateEndIndex);
                
int secondWeatherEndIndex = Html.IndexOf(" ", secondWeatherStartIndex + 24);
                
string SecondWeather = Html.Substring(secondWeatherStartIndex + 24, secondWeatherEndIndex  secondWeatherStartIndex  24);
                
//得到明天的温度

                
int secondTemperatureStartIndex = secondWeatherEndIndex + 1;
                
int secondTemperatureEndIndex = Html.IndexOf("</div>", secondTemperatureStartIndex);
                
string SecondTemperature = Html.Substring(secondTemperatureStartIndex, secondTemperatureEndIndex  secondTemperatureStartIndex);
                
int int4 = SecondTemperature.IndexOf(""0);
                
int int5 = SecondTemperature.IndexOf(""0);
                
int int6 = SecondTemperature.IndexOf("", int2);
                
string SecondMinTemperature = SecondTemperature.Substring(int5 + 1, int6  int5);
                
string SecondMaxTemperature = SecondTemperature.Substring(0, int5  int4 + 2);
                
//得到明天的风力
                int secondWindforceStartIndex = Html.IndexOf("风力:", secondTemperatureEndIndex);
                
int secondWindforceEndIndex = Html.IndexOf("</div>", secondWindforceStartIndex);
                
string SecondWindforce = Html.Substring(secondWindforceStartIndex + 3, secondWindforceEndIndex  secondWindforceStartIndex  3);
                
if (SecondWindforce.Contains(""))
                {
                }
                
else if (SecondWindforce.Contains("<"))                  //判断风力是否含有"<"或"≤"字样将,如果有的话,将其替换为2-
                {
                    
string strWindforce = SecondWindforce.Substring(1, FirstWindforce.Length  2);
                    
int minWindforce = Int32.Parse(strWindforce)  1;
                    SecondWindforce 
= SecondWindforce.Replace("<", minWindforce.ToString() + "");
                }
                
else if (SecondWindforce.Contains(""))
                {
                    
string strWindforce = SecondWindforce.Substring(1, SecondWindforce.Length  2);
                    
int minWindforce = Int32.Parse(strWindforce)  1;
                    SecondWindforce 
= SecondWindforce.Replace("", minWindforce.ToString() + "");
                }
                
#endregion
                
#region 得到后天的天气
                
//得到后天的标识跟城市
                int thirdDayAndCityStartIndex = Html.IndexOf("<h3>", secondWindforceEndIndex);
                
int thirdDayAndCityEndIndex = Html.IndexOf("</h3>", thirdDayAndCityStartIndex);
                
string thirdDayAndCity = Html.Substring(thirdDayAndCityStartIndex + 4, thirdDayAndCityEndIndex  thirdDayAndCityStartIndex  4);
                
//得到后天的日期跟星期
                int thirdDateStartIndex = Html.IndexOf("<p>", thirdDayAndCityEndIndex);
                
int thirdDateEndIndex = Html.IndexOf("</p>", thirdDateStartIndex);
                
string ThirdDate = Html.Substring(thirdDateStartIndex + 3, thirdDateEndIndex  thirdDateStartIndex  3).Replace("&nbsp;""");
                
//得到后天的天气
                int thirdWeatherStartIndex = Html.IndexOf("<div class=\"Weather_TP\">", thirdDateEndIndex);
                
int thirdWeatherEndIndex = Html.IndexOf(" ", thirdWeatherStartIndex + 24);
                
string ThirdWeather = Html.Substring(thirdWeatherStartIndex + 24, thirdWeatherEndIndex  thirdWeatherStartIndex  24);
                
//得到后天的温度

                
int thirdTemperatureStartIndex = thirdWeatherEndIndex + 1;
                
int thirdTemperatureEndIndex = Html.IndexOf("</div>", thirdTemperatureStartIndex);
                
string ThirdTemperature = Html.Substring(thirdTemperatureStartIndex, thirdTemperatureEndIndex  thirdTemperatureStartIndex);
                
int int7 = ThirdTemperature.IndexOf(""0);
                
int int8 = ThirdTemperature.IndexOf(""0);
                
int int9 = ThirdTemperature.IndexOf("", int2);
                
string ThirdMinTemperature = ThirdTemperature.Substring(int8 + 1, int9  int8);
                
string ThirdMaxTemperature = ThirdTemperature.Substring(0, int8  int7 + 2);
                
//得到后天的风力
                int thirdWindforceStartIndex = Html.IndexOf("风力:", thirdTemperatureEndIndex);
                
int thirdWindforceEndIndex = Html.IndexOf("</div>", thirdWindforceStartIndex);
                
string ThirdWindforce = Html.Substring(thirdWindforceStartIndex + 3, thirdWindforceEndIndex  thirdWindforceStartIndex  3);
                
if (ThirdWindforce.Contains(""))
                {
                }
                
else if (ThirdWindforce.Contains("<"))                  //判断风力是否含有"<"或"≤"字样将,如果有的话,将其替换为2-
                {
                    
string strWindforce = ThirdWindforce.Substring(1, ThirdWindforce.Length  2);
                    
int minWindforce = Int32.Parse(strWindforce)  1;
                    ThirdWindforce 
= ThirdWindforce.Replace("<", minWindforce.ToString() + "");
                }
                
else if (ThirdWindforce.Contains(""))
                {
                    
string strWindforce = ThirdWindforce.Substring(1, ThirdWindforce.Length  2);
                    
int minWindforce = Int32.Parse(strWindforce)  1;
                    ThirdWindforce 
= ThirdWindforce.Replace("", minWindforce.ToString() + "");
                }
                
#endregion
                al.Add(FirstDayAndCity);
                al.Add(FirstDate);
                al.Add(FirstWeather);
                al.Add(FirstMinTemperature);
                al.Add(FirstMaxTemperature);
                al.Add(FirstWindforce);
                al.Add(secondDayAndCity);
                al.Add(SecondDate);
                al.Add(SecondWeather);
                al.Add(SecondMinTemperature);
                al.Add(SecondMaxTemperature);
                al.Add(SecondWindforce);
                al.Add(thirdDayAndCity);
                al.Add(ThirdDate);
                al.Add(ThirdWeather);
                al.Add(ThirdMinTemperature);
                al.Add(ThirdMaxTemperature);
                al.Add(ThirdWindforce);
            }
            
catch (Exception err)
            {
            }
            
return al;
        }

代码下载:/Files/wlb/xinlangweather.rar

特 殊说明,使用第三种方法获取天气预报,输入城市的时候可能会受新浪提供的服务的影响,可能有些城市搜不到,前两种方法应该是不会受影响的。另外,由于代码 写的比较急,所以难免代码的质量就会有些问题,还请大家多多包涵。单纯从代码上看,可能确实没有什么难度,可是如果在您的工作中如果因为我的代码为您节省 了一些时间,笔者深感欣慰了。

另外,由于我的开发环境是vs2008+sp1,如果您的vs版本较低,不妨把项目文件删除掉,然后打开您的vs,选择打开网站,然后定位到本程序的目录,这样就可以进行代码的编译跟踪调试了。

再补充一点,有人可能对代码中出现的:System.Environment.NewLine 这句代码比较迷糊,因为这个是.net提供的简单的换行方法,我推荐大家以后能够使用这种.net提供给大家的高效、简便的方法。最关键的是它不容易出错。

作者:深山老林
出处:http://wlb.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏