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

这段代码展示了如何通过POST请求从配置的API获取天气预报和空气质量数据。它利用 ConfigurationManager 获取应用配置,设置请求参数,处理HTTPS证书验证,并读取响应内容。主要涉及网络请求、授权认证和JSON数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值