标签: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; }
标签:enc faq appcode city config orm author coding body
原文地址:https://www.cnblogs.com/xinbaba/p/8855263.html