首页 > 数据库技术 > 详细

c# 调取墨迹调用墨迹天气接口保存到数据库

时间:2018-04-16 12:37:09      阅读:455      评论:0      收藏:0      [点我收藏+]

一、墨迹接口调用

      

技术分享图片
 private String host = ConfigurationManager.AppSettings["WeatherHost"];
        private const String pathWeather = "/whapi/json/alicityweather/briefforecast3days";
        private const String method = "POST";
        private String appcode = ConfigurationManager.AppSettings["WeatherAppCode"];
        private const String pathAQI = "/whapi/json/alicityweather/briefaqi";
        private string GetWeatherORAQI(string path, int cityId = 2)
        {
            String querys = "";
            String bodys = "cityId=" + cityId;
            //String bodys = "cityId=2&token=677282c2f1b3d718152c4e25ed434bc4";//_zx
            String url = host + path;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + 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);
            //根据API的要求,定义相对应的Content-Type
            httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            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;
            }

            //Console.WriteLine(httpResponse.StatusCode);
            //Console.WriteLine(httpResponse.Method);
            //Console.WriteLine(httpResponse.Headers);
            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            return reader.ReadToEnd();
        }

        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
View Code

 

c# 调取墨迹调用墨迹天气接口保存到数据库

原文:https://www.cnblogs.com/xinbaba/p/8855263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!