Use WebService to call weather forecast

Search: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

This url is the webservice of the weather forecast service

1. Introduce web service references


2. Select the Advanced button


3. Select "Add web reference"


4.


5. Download the weather icon from http://www.webxml.com.cn/images/wather.zip , compress and save the weather file to vs (under the folder where the files are stored)

6. Add new item web page

7. Rendering

8. Background code

//Set an attribute record to output the string to the foreground
        public string8. MyWeather { get; set; }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var s = SWeather(txtWeather.Text.Trim());
            if (s[8]=="")
            {
                Response.Write("<script type='text/javascript'>alert('Can't find the current city')</script>");
                
            }
            else
            {
                MyWeather= getWeather(s);
            }

        }
        /// <summary>
        /// Returns an array of strings containing weather information
        /// </summary>
        /// <param name="str">User input city</param>
        /// <returns></returns>
        public string[] SWeather(string str)
        {
            Weather.WeatherWebService client = new Weather.WeatherWebService();
            var s = client.getWeatherbyCityName(str);//string array stores 22 returned results
            return s;
        }
        public string getWeather(string[] s)
        {
            StringBuilder sb = new StringBuilder();
            
            sb.Append(string.Format(@"<table border='1'>
                      <tr><td>今天天气概况:</td><td><span>{0}</span>  <span><img src=""Content/weather/{1}""/> <img src=""Content/weather/{2}""/></span></td></tr>
                     <tr><td>Today's live weather:</td><td>{3}</td></tr>
                     <tr><td>明天天气概况:</td><td><span>{4}</span>  <span><img src=""Content/weather/{5}""/> <img src=""Content/weather/{6}""/></span></td></tr>
                     <tr><td>后天天气概况:</td><td><span>{7}</span>  <span><img src=""Content/weather/{8}""/> <img src=""Content/weather/{9}""/></span></td></tr>
                </table>", s[6], s[8], s[9], s[10], s[13], s[15], s[16], s[18], s[20], s[21]));

            return sb.ToString();
        }
Code download address: https://download.csdn.net/download/wenshui147258/10349697

      Reference address: https://www.cnblogs.com/mengfei001/p/4324647.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324378326&siteId=291194637