当前位置:编程学习 > C#/ASP.NET >>

asp.net天气预报

 asp.net怎么实现这个
答案:使用web service
给你一个天气预报web service地址
www.webxml.com.cn/webservices/weatherwebservice.asmx
这个有时间限制,每天最多访问多少次,
你可以把访问结果缓存30分钟,缓存失效后再进行请求
其他:可以给你思路 
做个定时触发的 去某些大的天气预报网站 抓取数据

更新回来 找借口,直接访问天气预报借口就行了。。。
接口网上找去吧。 调用WebServices接口,网上有很多。也有实例,搜索一下有很多的。。。
只能说给你指了路,后面的自己走吧 用Web Service实现,有结口 网上有个webservice的天气预报的例子,你可以下下来参照一下,调用webservice服务。我这里有个写好的,可以给你。 就是提供web server的服务器挂掉了, 调用接口..去天气预报网调用web Service 接口就行了 很容易的 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Collections;
/// <summary>
/// Weather 的摘要说明
/// </summary>
public class Weather
{
    public Weather()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static string ConvertCodeByCity(string City)
    {
        string Code = "";
        switch (City)
        {
            case "北京":
                Code = "110100";
                break;
           
            default:
                break;
        }
        return Code;
    }

    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("" + 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;
    }

}
 

上一个:c# asp.net 关于命名空间 和 类 使用的问题
下一个:asp.net里Repeater与AspNetpager怎么配合使用的?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,