根据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, @"來自: (.+) ");
??????????????????? 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添加服務引用)
??????????????????
?? 二、在輸入框中輸入下面的網址:
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 + " " + 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;
?
??????????????? }
這樣速度加快了很多。
?
轉載于:https://www.cnblogs.com/honghong75042/archive/2012/04/12/2443532.html
總結
以上是生活随笔為你收集整理的根据ip地址获得天气预报的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: List.FindAll 方法
- 下一篇: 开发管理 (3) -项目启动会议