[Route("api/Weather/weather")]
[HttpGet]
public Reponse<WeatherRes> weather(string city)
{
var JsonStr = Get("http://wthrcdn.etouch.cn/weather_mini?city=" + city);
var result = JsonConvert.DeserializeObject<WeatherRes>(JsonStr);
return Success(result);
}
public string Get(string url)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Stream stream = resp.GetResponseStream();//原来是这个 但是服务器做了数据压缩 下面做数据解压
Stream stream = new System.IO.Compression.GZipStream(resp.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
try
{
//获取内容
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
finally
{
stream.Close();
}
return result;
}
根据城市名称获取天气信息
最新推荐文章于 2024-07-02 17:50:43 发布