首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

简单有关问题。网站接口小弟我如何调用

2013-06-25 
简单问题。网站接口我怎么调用?例如给了我一个网站接口,http://m.weather.com.cn/data/101010100.html。显示

简单问题。网站接口我怎么调用?
例如给了我一个网站接口,http://m.weather.com.cn/data/101010100.html。显示北京的天气的。我想把它做成天气预报。怎么让软件一打开就自动登陆这个网站把数据包拿下来?? 软件
[解决办法]
webclient直接downstring,返回的是json串,找个解析json的类去处理,下载一个 Newtonsoft.Json.dll
[解决办法]
用httpwebrequest获取源码,然后json解析结果就ok了
[解决办法]



//天气信息来自 中国天气网 !
//很早以前写过一段

//**************************************************** 

protected void Page_Load(object sender, EventArgs e)
    {

        string str = GetResponseStr();//获得json天气信息
        //对json格式的信息重新整理一下,因为我们要用.net自带的方法获得相关信息
        str = str.Remove(str.LastIndexOf("}"), 1);
        str = str.Remove(0, 15);

        //使用JavaScriptSerializer对象来解析数据
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str);

        object str_city;        //-- 城市 -- city
        object str_weather;     //-- 天气 -- weather1
        object str_img_id;      //-- 天气图标编号 -- img1
        object str_temp;        //-- 温度 -- temp1
        object str_fl;          //-- 风力 --fl1
        object str_UV;          //-- 紫外线 -- index_uv
        object str_cy;          //-- 穿衣指数 -- index
        object str_xc;          //-- 洗车指数 -- index_xc
        object str_CO;          //-- 舒适指数 -- index_co
        object str_Time;        //-- 时间 -- date_y
        object str_week;        //-- 星期 -- week

        if (json.TryGetValue("city", out str_city))
        {
            Response.Write (str_city.ToString());//城市


        }
        if (json.TryGetValue("weather1", out str_weather))
        {
            //Response.Write(str_weather.ToString());//天气
            lab_Tianqi.Text = str_weather.ToString();
        }
        if (json.TryGetValue("temp1", out str_temp))
        {
            //Response.Write(str_temp.ToString());//温度
            lab_wendu.Text = str_temp.ToString();
        }
        if (json.TryGetValue("fl1", out str_fl))
        {
            //Response.Write(str_fl.ToString());//风力
            lab_Fengli.Text = str_fl.ToString();
        }
        if (json.TryGetValue("index_uv", out str_UV))
        {
            //Response.Write(str_UV.ToString());//紫外线
            lab_UV.Text = str_UV.ToString();
        }
        //if (json.TryGetValue("index", out str_cy))
        //{
        //    Response.Write(str_cy.ToString());//穿衣
        //}
        if (json.TryGetValue("index_xc", out str_xc))
        {
            //Response.Write(str_xc.ToString());//洗车
            lab_Xc.Text = str_xc.ToString();
        }
        if (json.TryGetValue("index_co", out str_CO))
        {
            //Response.Write(str_CO.ToString());//舒适
            lab_Co.Text = str_CO.ToString();
        }

        if (json.TryGetValue("date_y", out str_Time))
        {
           // Response.Write(str_Time.ToString());//时间
            lab_Time.Text = str_Time.ToString();
        }
        if (json.TryGetValue("week", out str_week))
        {
            //Response.Write(str_week.ToString());//星期
            lab_Week.Text = str_week.ToString();


        }

        if (json.TryGetValue("img1", out str_img_id))
        {
            //Response.Write(str_img_id.ToString());//图片编号
            img.Src = "http://m.weather.com.cn/img/b" + str_img_id + ".gif";
            img.Width = 50;
            img.Height = 50;
        }

    }

    /// <summary>
    /// 获得天气的信息(json格式)
    /// </summary>
    /// <returns></returns>
    private string GetResponseStr()
    {
        string str = "http://m.weather.com.cn/data/101010100.html";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();//获得回应的数据流
        //将数据流转成 String 
        string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
        return result;
    }

热点排行