Click on the link to see China Weather Network interface Description, recently want to study the interface test, from the most recent talk with a company's technical director (Jiaotong University alumni), found that the requirements of interface testing is relatively large, so want to study.
Finally found on the internet on the Chinese Weather Network interface Description, is the above article. Decide to access this interface, analyze the content returned by the interface, and filter out the information I want.
The thought of the interface was mainly JSON type, but I did not find the JSON type interface (above the JSON interface seems to be invalid), first take the XML type interface test, I think the principle should be similar, but the specific implementation is somewhat different.
If anyone has a JSON interface available for easy disclosure, welcome message, thank you very much!
The following is an example of the weather information for Beijing city from the content returned from the China Weather Network interface, the code is as follows:
usingSystem;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingSystem.Xml.Linq;namespaceinterfacetest{classProgram {Static voidMain (string[] args) {Reportweather ("Beijing"); } Private Static voidReportweather (stringCityName) { stringLocalcontent = Environment.getfolderpath (Environment.SpecialFolder.DesktopDirectory) +@"\test.txt"; Try { stringURL ="Http://flash.weather.com.cn/wmaps/xml/china.xml"; WebClient mywebclient=NewWebClient ();byte[] pagedata = mywebclient.downloaddata (URL);stringpagehtml = Encoding.UTF8.GetString (pagedata);
//XDocument doc = Xdocument.parse (pagehtml); using(StreamWriter SW =NewStreamWriter (localcontent)) {SW. WriteLine (pagehtml); } XDocument Doc=Xdocument.parse (pagehtml); //XDocument doc = xdocument.load (localcontent); varResults = fromEleinchDoc. Descendants (" City")whereEle. Attribute ("CityName"). Value.equals (CityName)SelectEle; foreach(varResultinchresults) {Console.WriteLine ("City:"+cityname); Console.WriteLine ("Weather conditions:"+ result. Attribute ("statedetailed"). Value); Console.WriteLine ("temperature conditions (degrees Celsius):"+ result. Attribute ("tem1"). Value +"to the"+ result. Attribute ("tem2"). Value); Console.WriteLine ("Wind Conditions:"+ result. Attribute ("windstate"). Value); } console.readline (); } Catch(WebException webEx) {Console.WriteLine (webEx.Message.ToString ()); } } }}
For the sake of convenience, I saved the information returned by the interface to a local copy (path Localcontent), which reads as follows:
Run as follows (cloudy turn haze ...) I remember listening to the weather forecast is cloudy and sunny ... Alas...... ):
This allows us to filter the return information of the XML interface and test the XML type interface.
C # Implementation of Chinese Weather Network XML Interface test