根据ip地址获得天气预报
?????根據(jù)訪問(wèn)者的ip地址獲得當(dāng)?shù)氐奶鞖馇闆r,最簡(jiǎn)單的一種方法就是加一個(gè)iframe框架方法如下:
<iframe src="http://m.weather.com.cn/m/pn12/weather.htm" frameborder="0" width="200px" height="110px" scrolling="no"></iframe>
如果想得到比較詳細(xì)的頁(yè)面可以點(diǎn)擊這個(gè)網(wǎng)址:http://weather.xixik.com/
這樣方法雖然簡(jiǎn)單,但是有一個(gè)很大的缺點(diǎn)就是在IE瀏覽器下它的背景色是白色的(谷歌瀏覽器的背景色是透明的)如果你想將他顯示在有背景色的條上會(huì)出現(xiàn)這種效果:
為了解決這種缺點(diǎn)可以使用第二種方法通過(guò)天氣預(yù)報(bào)的WebService接口調(diào)用:
思想:1、先找確定訪問(wèn)者所在的地區(qū)。
??????????????????? string strResponse = GetPageData("http://www.123cha.com/");
??????????????????? Match match = Regex.Match(strResponse, @"來(lái)自: (.+) ");
??????????????????? string[] addresses = match.Groups[1].Value.Replace(" ", " ").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添加服務(wù)引用)
??????????????????
?? 二、在輸入框中輸入下面的網(wǎng)址:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx? 然后點(diǎn)擊前往,記下引用名(引用名可以自己改寫(xiě))
?????????????????
?????????????????? 最后代碼:
??????????????????? Weather.WeatherWebService w = new Weather.WeatherWebService();
??????????????????? //把webservice當(dāng)做一個(gè)類來(lái)操作?
??????????????????? string[] s = new string[23];//聲明string數(shù)組存放返回結(jié)果?
??????????????????? string city = address;//獲得文本框錄入的查詢城市?
??????????????????? s = w.getWeatherbyCityName(city);
??????????????????? //以文本框內(nèi)容為變量實(shí)現(xiàn)方法getWeatherbyCityName?
??????????????????? if (s[8] == "")
??????????????????? {
??????????????????????? //MessageBox.Show("暫時(shí)不支持您查詢的城市");
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? string st = s[10];
??????????????????? }
?提升:
1、這樣下來(lái)雖然效果實(shí)現(xiàn)了,但是加載速度很慢。為了不讓它影響其它頁(yè)面的加載速度,我將獲取天氣預(yù)報(bào)這塊單獨(dú)寫(xiě)了一個(gè)頁(yè)面。然后在需要調(diào)用天氣的預(yù)報(bào)的頁(yè)面加了一個(gè)Frame框架。這樣天氣預(yù)報(bào)雖然加載慢但是卻不會(huì)影響其他頁(yè)面。
2、有好幾個(gè)頁(yè)面都用到這個(gè)天氣預(yù)報(bào),我沒(méi)必要換一個(gè)頁(yè)面就去用WebService去調(diào)用天氣情況。針對(duì)這樣情況,我采取只第一次加載的方法。然后將獲得天氣預(yù)報(bào)放到Cookie里這樣其他頁(yè)面再使用的時(shí)候我可以直接從cookie里調(diào)用省了很多時(shí)間。
具體代碼如下:
? string content = "<table><tr id='temprature' runat='server'><td>" + dt + " " + week + "<td>" + address + ": " + 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 + " " + week + "<td>" + address + ": " + tian + "</td></tr></table>";
??????????????????? temprature.InnerHtml = content;
?
??????????????? }
這樣速度加快了很多。
?
轉(zhuǎn)載于:https://www.cnblogs.com/honghong75042/archive/2012/04/12/2443532.html
總結(jié)
以上是生活随笔為你收集整理的根据ip地址获得天气预报的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: List.FindAll 方法
- 下一篇: 开发管理 (3) -项目启动会议