码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-04-16 13:01:38      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:enc   faq   appcode   city   config   orm   author   coding   body   

一、墨迹接口调用

      

技术分享图片
 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# 调取墨迹调用墨迹天气接口保存到数据库

标签:enc   faq   appcode   city   config   orm   author   coding   body   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!