1024programmer Asp.Net .NET7 uses HttpClient to realize query weather forecast interface

.NET7 uses HttpClient to realize query weather forecast interface

.NET7 uses HttpClient to implement query weather forecast interface

A friend needs to display the weather forecast according to the city to make a website. After searching around, he couldn’t find a reliable interface. Today, he found an interface in a certain city on the official website of the Central Meteorological Observatory. First, he tried it with postman and it actually worked. You can check it 7-day weather forecast and other information for a certain city. But the query code is the code of the Meteorological Observatory itself. I searched the Internet and found this code. This article uses the HttpClient method to query this interface.

Weather interface

 Image

City Code

HttpClient is the most commonly used http request tool on the .net core platform. It is developed directly based on Socket and provides an asynchronous and friendly code writing method. It is under the System.Net.Http namespace.

1. Create a new environment

First create a .NET7 Asp.net core web Api program. The options are as follows.

picture

Second, create a new GetWeather in the controller

Here we use the get method to request, query the weather forecast by passing in the city name, put the city name and code into the Dictionary, and use a single method to query. The code is as follows:

 [HttpGet(Name = "GetWeather")]
 public async Task GetWeather(string cityName)
  {
 //Coding query uses a single method
 int citycode = publicfun. GetKeys(cityName);
      using var client = new HttpClient();
      //The base address of the request, usually the domain name
      client.BaseAddress = new Uri("http://www.nmc.cn");
      //In the request header, we specify the User-Agent
      client.DefaultRequestHeaders.Add("User-Agent", "CJAVAPY BOT");
      client.DefaultRequestHeaders.Accept.Add(
            // Set the accept header to tell JSON is an acceptable response type
              new MediaTypeWithQualityHeaderValue("application/json"));
 //actual request address
      var url = $"rest/weather?stationid={citycode}&_=1672315767048";
 HttpResponseMessage response = await client.GetAsync(url);//get request
      response.EnsureSuccessStatusCode();
 var result= await response.Content.ReadAsStringAsync();//Get the result
 return JsonResult(result) ;//return json
  }
 

The coding dictionary method of each city in the country is as follows

 public static class publicfun
     {
         public static int GetKeys(string key)
         {
             Dictionary dic = new Dictionary();
             dic.Add("Macao", 45011);
             dic.Add("Daxinganling", 50442);
             dic.Add("Heihe", 50468);
             dic.Add("Qiqihar", 50745);
             dic.Add("Yichun", 50774);
             dic.Add("Hegang", 50775);
             dic.Add("Suihua", 50853);
             dic.Add("Jiamusi", 50873);
             dic.Add("Shuangyashan", 50884);
             dic.Add("White City", 50936);
             dic.Add("Harbin", 50953);
             dic.Add("Qitaihe", 50973);
             dic.Add("Jixi", 50978);
             dic.Add("Altay", 51076);
             dic.Add("Tower City", 51133);
             dic.Add("Bozhou", 51238);
             dic.Add("Karamay", 51243);
             dic.Add("Shihezi", 51356);
             dic.Add("Changji", 51368);
             dic.Add("Yining", 51431);
             dic.Add("Urumqi", 51463);
             dic.Add("Turpan", 51573);
             dic.Add("Aksu", 51628);
             dic.Add("Bazhou", 51656);
             dic.Add("Kezhou", 51704);
             dic.Add("Kashgar", 51709);
             dic.Add("Hetian", 51828);
             dic.Add("Hami", 52203);
             dic.Add("Jiuquan", 52533);
             dic.Add("Zhangye", 52652);
             dic.Add("Jinchang", 52675);
             dic.Add("Wuwei", 52679);
             dic.Add("Xining", 52866);
             dic.Add("Pingan County", 52875);
             dic.Add("Lanzhou", 52889);
             dic.Add("Silver", 52896);
             dic.Add("Tongren County", 52974);
             dic.Add("Linxia", 52984);
             dic.Add("Dingxi", 52995);
             dic.Add("Baotou", 53446);
             dic.Add("Hohhot", 53463);
             dic.Add("Datong", 53487);
             dic.Add("Wuhai", 53512);
             dic.Add("Shizuishan", 53518);
             dic.Add("Suozhou", 53578);
             dic.Add("Wu Zhong", 53612);Yong", 57558);
             dic.Add("Yueyang", 57584);
             dic.Add("Xianning", 57590);
             dic.Add("Changde", 57662);
             dic.Add("Yiyang", 57674);
             dic.Add("Changsha", 57687);
             dic.Add("Bijie", 57707);
             dic.Add("Zunyi", 57713);
             dic.Add("Tongren", 57741);
             dic.Add("Huaihua", 57749);
             dic.Add("Loudi", 57763);
             dic.Add("Shaoyang", 57766);
             dic.Add("Xiangtan", 57773);
             dic.Add("Zhuzhou", 57780);
             dic.Add("Pingxiang", 57786);
             dic.Add("Yichun", 57793);
             dic.Add("Xinyu", 57796);
             dic.Add("Anshun", 57806);
             dic.Add("Guiyang", 57816);
             dic.Add("Kerry", 57825);
             dic.Add("Duyun", 57827);
             dic.Add("Hengyang", 57872);
             dic.Add("Guilin", 57957);
             dic.Add("Chenzhou", 57972);
             dic.Add("Ganzhou", 57993);
             dic.Add("Shangqiu", 58005);
             dic.Add("Zaozhuang", 58024);
             dic.Add("Xuzhou", 58027);
             dic.Add("Lianyungang", 58044);
             dic.Add("Bozhou", 58102);
             dic.Add("Huaibei", 58116);
             dic.Add("Suzhou", 58122);
             dic.Add("Suqian", 58131);
             dic.Add("Huai'an", 58145);
             dic.Add("Yancheng", 58151);
             dic.Add("Fuyang", 58203);
             dic.Add("Bengbu", 58221);
             dic.Add("Huainan", 58224);
             dic.Add("Chuzhou", 58236);
             dic.Add("Nanjing", 58238);
             dic.Add("Yangzhou", 58245);
             dic.Add("Taizhou", 58246);
             dic.Add("Zhenjiang", 58248);
             dic.Add("Nantong", 58259);
             dic.Add("Six'an", 58311);
             dic.Add("Hefei", 58321);
             dic.Add("Chaohu", 58326);
             dic.Add("Wuhu", 58334);
             dic.Add("Ma'anshan", 58336);
             dic.Add("Changzhou", 58343);
             dic.Add("Wuxi", 58354);
             dic.Add("Suzhou", 58357);
             dic.Add("Shanghai", 58362);
             dic.Add("Yellowstone", 58407);
             dic.Add("Anqing", 58424);
             dic.Add("Chizhou", 58427);
             dic.Add("Tongling", 58429);
             dic.Add("Xuancheng", 58433);
             dic.Add("Huzhou", 58450);
             dic.Add("Jiaxing", 58452);
             dic.Add("Shaoxing", 58453);
             dic.Add("Hangzhou", 58457);
             dic.Add("Zhoushan", 58477);
             dic.Add("Jiujiang", 58502);
             dic.Add("Jingdezhen", 58527);
             dic.Add("Jinhua", 58549);
             dic.Add("Nanchang", 58606);
             dic.Add("Fuzhou", 58617);
             dic.Add("Yingtan", 58627);
             dic.Add("Quzhou", 58633);
             dic.Add("Lishui", 58646);
             dic.Add("Wenzhou", 58659);
             dic.Add("Taizhou", 58660);
             dic.Add("Sanming", 58828);
             dic.Add("Nanping", 58834);
             dic.Add("Ningde", 58846);
             dic.Add("Fuzhou", 58847);
             dic.Add("Longyan", 58927);
             dic.Add("Putian", 58946);
             dic.Add("Hechi area", 59023);
             dic.Add("Liuzhou", 59046);
             dic.Add("Hezhou area", 59065);
             dic.Add("Shaoguan", 59082);
             dic.Add("Zhangzhou", 59126);
             dic.Add("Quanzhou", 59131);
             dic.Add("Xiamen", 59134);
             dic.Add("Baise", 59211);
             dic.Add("Guest", 59242);
             dic.Add("Guigang", 59249);
             dic.Add("Wuzhou", 59265);
             dic.Add("Zhaoqing", 59278);
             dic.Add("Qingyuan", 59280);
             dic.Add("Guangzhou", 59287);
             dic.Add("Foshan", 59288);
             dic.Add("Dongguan", 59289);
             dic.Add("Heyuan", 59293);
             dic.Add("Chaozhou", 59312);
             dic.Add("Jieyang", 59315);
             dic.Add("Shantou", 59316);
             dic.Add("Chongzuo", 59425);
             dic.Add("Yulin", 59453);
             dic.Add("Yunfu", 59471);
             dic.Add("Zhongshan", 59485);
             dic.Add("Zhuhai", 59488);
             dic.Add("Shenzhen", 59493);
             dic.Add("Shanwei", 59501);
             dic.Add("Kaohsiung", 59554);
             dic.Add("Qinzhou", 59632);
             dic.Add("Fangchenggang", 59635);
             dic.Add("Beihai", 59644);
             dic.Add("Zhanjiang", 59658);
             dic.Add("Maoming", 59659);
             dic.Add("Yangjiang", 59663);
             dic.Add("Haikou", 59758);
             dic.Add("Sanya", 59948);
             dic.Add("Xisha", 59981);
 var rel = dic[key];
             return rel;
         }
     }
 

The query results are as follows:

image

All the data is available, and you can create an object according to the actual needs to obtain the required data, which is not implemented here.

3. Conclusion

This article describes the use of .NET7 to query the weather forecast through the get method of HttpClient. The time relationship does not realize all functions, and it is for reference only. If there is no requirement for the style, there is a js interface on the Internet, and it can be pasted directly on the web page. Do you have any good weather forecast API? Welcome to leave a message to discuss.

Source public account: DotNet development job-hopping❀            
 

The get method of nt queries the weather forecast, and the time relationship does not realize all functions, it is for reference only. If there is no requirement for the style, there is a js interface on the Internet, and it can be pasted directly on the web page. Do you have any good weather forecast API? Welcome to leave a message to discuss.

Source public account: DotNet development job-hopping❀            
 
This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/net7-uses-httpclient-to-realize-query-weather-forecast-interface/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索