C # Call WebService get weather information

Source: Internet
Author: User

Overview

This article uses C # to develop WinForm applications that get weather forecast data by invoking the WebService service Weatherws of <WebXml/> (url:http://www.webxml.com.cn). The URL of the Web service used by this program is:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx, the service is "more than 2,400 city weather forecast Web services ."

Development Environment Description:

    • System Platform: Windows 7 (32bit);
    • Development tools: VS2010;
implementation Process

This program implements the invocation of the WebService service in two ways: Add Web Reference and use WSDL file.

1. Add a Web reference

First, create a new WinForm application and add a Web reference to the project in Solution Manager: Right-click the project and add a service reference, which pops up the following Service Reference Settings dialog box:

  

Click the "Add Web Reference" button in the dialog box to bring up the Web Reference dialog box where you enter the WEATHERWS service address at the URL (http://ws.webxml.com.cn/WebServices/WeatherWS.asmx ), click the Go To button, and modify the Web reference named "Webrefweather" as shown in:

  

At this point, add "Get weather core code" where you need to get the weather information. I added this in the button response function with the following code:

Webrefweather.weatherws weather =NewWebrefweather.weatherws ();string[] str =New string[ +];Try{str= Weather.getweather ("Beijing",""); MessageBox.Show (str[0] +"\ n"+ str[1] +"\ n"+ str[2] +"\ n"+ str[4] +"\ n"+ str[5],"Weather Information");}Catch(Exception ex) {MessageBox.Show (ex). Message);}

After the program runs, click the button to display the weather information, as shown in:

  

2. Using WSDL file

This method is used by the Web service URL by using the VS tool (http://ws.webxml.com.cn/WebServices/ Weatherws.asmx) or a local weatherws.asmx file to get the WSDL file, and then the WSDL file generates the CS file, the Web service proxy class, and finally gets the weather data by using this class. That's a few steps:

    • ASMX file--WSDL file (VS2010 tool:disco);
    • WSDL file--CS file (VS2010 tool:wsdl);

First, look at the help of the Disco tool, as shown in:

  

Get the WSDL file by using the following command:

Disco Http://ws.webxml.com.cn/WebServices/WeatherWS.asmx

As shown in the following:

  

Then, using the WSDL command to generate the CS file from the WSDL file, the WSDL command helps as follows:

  

The command to generate the CS file is as follows:

WSDL/L: CS /n: Ns_weatherws /out: WEATHERWS. CS weatherws.wsdl

That

  

At this point, add the CS file to the new WinForm project, and then add the following core code to the button's response function:

Ns_weatherws.weatherws weather =NewNs_weatherws.weatherws ();string[] str =New string[ +];Try{str= Weather.getweather ("Beijing",""); MessageBox.Show (str[0] +"\ n"+ str[1] +"\ n"+ str[2] +"\ n"+ str[4] +"\ n"+ str[5],"Weather Information");}Catch(Exception ex) {MessageBox.Show (ex). Message);}

At this point, the following error occurs when you run the program:

The type or namespace name "Services" does not exist in the namespace "system.web". Is the assembly missing?

Workaround: Add dotnet Reference System.Web.Services to the project as shown in:

  

After you add the program, the program starts successfully. Then, clicking the button will display the weather information as in the previous method, as shown in:

  

C # Call WebService get weather information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.