根据ip地址获得天气预报

     根据访问者的ip地址获得当地的天气情况,最简单的一种方法就是加一个iframe框架方法如下:

<iframe src="http://m.weather.com.cn/m/pn12/weather.htm" frameborder="0" width="200px" height="110px" scrolling="no"></iframe>

如果想得到比较详细的页面可以点击这个网址:http://weather.xixik.com/

这样方法虽然简单,但是有一个很大的缺点就是在IE浏览器下它的背景色是白色的(谷歌浏览器的背景色是透明的)如果你想将他显示在有背景色的条上会出现这种效果:

为了解决这种缺点可以使用第二种方法通过天气预报的WebService接口调用:

思想:1、先找确定访问者所在的地区。

                    string strResponse = GetPageData("http://www.123cha.com/");
                    Match match = Regex.Match(strResponse, @"来自:&nbsp;&nbsp;(.+)&nbsp;");
                    string[] addresses = match.Groups[1].Value.Replace("&nbsp;", " ").Split(' ');//获得Ip所在的城市
                    string address = "北京";
                    int n = 0;
                    if (addresses.Length >= 3)
                    {
                        n = 1;
                    }
                    address = addresses[n].Substring(0, addresses[n].Length - 1);

           2、添加WebService引用:

         添加webService的方法:

       一、添加Web引用(如果是2010添加服务引用)

                  

         二、在输入框中输入下面的网址:

       http://www.webxml.com.cn/WebServices/WeatherWebService.asmx  然后点击前往,记下引用名(引用名可以自己改写)

       

                 

                   最后代码:
                    Weather.WeatherWebService w = new Weather.WeatherWebService();
                    //把webservice当做一个类来操作 
                    string[] s = new string[23];//声明string数组存放返回结果 
                    string city = address;//获得文本框录入的查询城市 
                    s = w.getWeatherbyCityName(city);
                    //以文本框内容为变量实现方法getWeatherbyCityName 
                    if (s[8] == "")
                    {
                        //MessageBox.Show("暂时不支持您查询的城市");
                    }
                    else
                    {
                        string st = s[10];
                    }

 提升:

1、这样下来虽然效果实现了,但是加载速度很慢。为了不让它影响其它页面的加载速度,我将获取天气预报这块单独写了一个页面。然后在需要调用天气的预报的页面加了一个Frame框架。这样天气预报虽然加载慢但是却不会影响其他页面。

2、有好几个页面都用到这个天气预报,我没必要换一个页面就去用WebService去调用天气情况。针对这样情况,我采取只第一次加载的方法。然后将获得天气预报放到Cookie里这样其他页面再使用的时候我可以直接从cookie里调用省了很多时间。

具体代码如下:

  string content = "<table><tr id='temprature' runat='server'><td>" + dt + "&nbsp;" + week + "<td>" + address + ":&nbsp;" + s[10] + "</td></tr></table>";
                    HttpCookie wea = new HttpCookie("weather");
                    wea.Values["date"] = dt;
                    wea.Values["week"] = week;
                    wea.Values["address"] = address;
                    wea.Values["tian"] = s[10];
                    Response.Cookies.Add(wea);
                    temprature.InnerHtml = content;

if (Request.Cookies["weather"] != null)
                {
                    HttpCookie wea = Request.Cookies["weather"];
                    string dt = wea.Values["date"];
                    string week=wea.Values["week"];
                    string address=wea.Values["address"];
                    string tian=wea.Values["tian"];
                    string content = "<table><tr id='temprature' runat='server'><td>" + dt + "&nbsp;" + week + "<td>" + address + ":&nbsp;" + tian + "</td></tr></table>";
                    temprature.InnerHtml = content;

                }

这样速度加快了很多。

原文地址:https://www.cnblogs.com/honghong75042/p/2443532.html