阿里云天气预报接口代码

来源:互联网 发布:工控软件开发是什么 编辑:程序博客网 时间:2024/06/10 05:18
        private const String host = "http://jisutqybmf.market.alicloudapi.com";
        private const String path = "/weather/";
        private const String method = "GET";

        private const String appcode = "edc41a0bfae74864b90ce267502e029a";


                String querys = "city=枣庄";
                String bodys = "";
                String url = host + path;
                HttpWebRequest httpRequest = null;
                HttpWebResponse httpResponse = null;




                if (0 < querys.Length)
                {
                    //  url = url + "/city";
                    url = url + "query?" + querys;
                }


                if (host.Contains("https://"))
                {
                    //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(url);
                }
                httpRequest.Method = method;
                httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
                if (0 < bodys.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    httpResponse = (HttpWebResponse)ex.Response;
                }
                string s1 = httpResponse.StatusCode.ToString() + httpResponse.StatusCode.ToString() + httpResponse.Method + httpResponse.Headers;


                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                string s2 = reader.ReadToEnd();
                // Console.WriteLine(reader.ReadToEnd());
                //  Console.WriteLine("\n");


                int d1 = s2.IndexOf("\"daily\":[");
                int d2 = s2.IndexOf("\"hourly\":[");
                string s3 = s2.Substring(d1 + 8, d2 - d1 - 9);


                ///
                 //s3 = "[{date:2017-03-21,week:星期二,sunrise:06:12,sunset:18:22,night:{weather:多云,templow:5,img:1,winddirect:无持续风向,windpower:微风},day:{weather:晴,temphigh:16,img:0,winddirect:无持续风向,windpower:微风}},{date:2017-03-22,week:星期三,sunrise:06:11,sunset:18:23,night:{weather:小雨,templow:4,img:7,winddirect:无持续风向,windpower:微风},day:{weather:多云,temphigh:11,img:1,winddirect:无持续风向,windpower:微风}},{date:2017-03-23,week:星期四,sunrise:06:09,sunset:18:24,night:{weather:小雨,templow:6,img:7,winddirect:无持续风向,windpower:微风},day:{weather:小雨,temphigh:11,img:7,winddirect:无持续风向,windpower:微风}},{date:2017-03-24,week:星期五,sunrise:06:08,sunset:18:25,night:{weather:多云,templow:5,img:1,winddirect:无持续风向,windpower:微风},day:{weather:多云,temphigh:12,img:1,winddirect:无持续风向,windpower:微风}},{date:2017-03-25,week:星期六,sunrise:06:07,sunset:18:26,night:{weather:多云,templow:3,img:1,winddirect:无持续风向,windpower:微风},day:{weather:多云,temphigh:16,img:1,winddirect:无持续风向,windpower:微风}},{date:2017-03-26,week:星期日,sunrise:07:30,sunset:19:30,night:{weather:晴,templow:3,img:0,winddirect:西北风,windpower:4级},day:{weather:多云,temphigh:16,img:1,winddirect:西北风,windpower:4级}},{date:2017-03-27,week:星期一,sunrise:07:30,sunset:19:30,night:{weather:晴,templow:4,img:0,winddirect:西北风,windpower:微风},day:{weather:晴,temphigh:18,img:0,winddirect:西北风,windpower:微风}}]";
                s3 = s3.Replace("\"", "");
                dt1.Columns.Add("date");
                dt1.Columns.Add("week");
                dt1.Columns.Add("weat");
                dt1.Columns.Add("low");//templow
                dt1.Columns.Add("high");//temphigh
                dt1.Columns.Add("RiQi");//temphigh
                while (true)
                {


                    DataRow dr1 = dt1.NewRow();
                     d1 = s3.IndexOf("date");
                    if (d1 > -1)
                    {
                        dr1[0] = s3.Substring(d1 + 5, 10);
                        dr1[0] = dr1[0].ToString().Replace(",", "");
                    }
                    else
                    {
                        break;
                    }
                    d1 = s3.IndexOf("week");
                    if (d1 > -1)
                    {
                        dr1[1] = s3.Substring(d1 + 5, 3);
                        dr1[1] = dr1[1].ToString().Replace(",", "");
                    }


                    d1 = s3.IndexOf("weather");
                    if (d1 > -1)
                    {
                        string s4 = s3.Substring(d1 + 8, 10);
                        d1 = s4.IndexOf(",");
                        if (d1 > -1)
                        {
                            s4 = s4.Substring(0, d1);
                        }
                        dr1[2] = s4;
                        
                        dr1[2] = dr1[2].ToString().Replace(",", "");
                    }


                    d1 = s3.IndexOf("templow");
                    if (d1 > -1)
                    {
                        dr1[3] = s3.Substring(d1 + 8, 2);
                        dr1[3] = dr1[3].ToString().Replace(",", "");
                    }
                    d1 = s3.IndexOf("temphigh");
                    if (d1 > -1)
                    {
                        dr1[4] = s3.Substring(d1 + 9, 2);
                        dr1[4] = dr1[4].ToString().Replace(",", "");
                    }


                    dt1.Rows.Add(dr1);
                    s3 = s3.Substring(d1 + 10);
                }
                dt1.Columns.Add("t");
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    DateTime dtime2 = DateTime.Parse(dt1.Rows[i]["date"].ToString());
                    dt1.Rows[i]["RiQi"] = dtime2.ToString("yyyy年MM月dd日");
                }
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    for (int j = 0; j < dt0.Rows.Count; j++)
                    {
                        if (dt1.Rows[i]["date"].ToString().Trim() == dt0.Rows[j]["date"].ToString().Trim())
                        {
                            dt1.Rows[i]["t"] = "1";
                            break;
                        }
                    }
                }


具体内容参看:


0 0
原创粉丝点击