天天看點

墨迹天氣接口html,c# 調取墨迹調用墨迹天氣接口儲存到資料庫

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

{

url= url + "?" +querys;

}if (host.Contains("https://"))

{

ServicePointManager.ServerCertificateValidationCallback= newRemoteCertificateValidationCallback(CheckValidationResult);

httpRequest= (HttpWebRequest)WebRequest.CreateDefault(newUri(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

{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"));returnreader.ReadToEnd();

}public static bool CheckValidationResult(objectsender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

{return true;

}