Traditional Culture Encyclopedia - Weather forecast - How to call the webservice interface

How to call the webservice interface

Webservice is mainly a method written by some websites for us to call. Of course, we can also write our own webservice, so we must first find such an interface. See if some websites have such an interface.

Let's look at a simple weather forecast interface. First, enter the address of the weather forecast interface in the address bar: (/weatherService.asmx This is an example and can be modified according to personal needs).

There will be some method names and parameter descriptions with links. After reading it, you can go to the link to watch the test. You can write the parameters you want to test first, click Call, and an xml file will appear. These are the results. Depending on the situation, we just need to process the results into what we want.

Let's talk about how to call it in the project: create a new web project, then click Add Reference Service as shown in the figure, and then click OK.

In this way, you will find that there are many nodes in the webconfig file, and there is a green thing in the service reference file in the project class. You can click to see if he has any method, which should be consistent with the way we enter the link in the browser, and the rest is how to call it. The specific code is as follows: this code value first adds a Lable, a TextBox, a Button and a Literal to the webForm. Then click the button.

Protected void Button 1_Click (object sender, EventArgs e)

{

If (! String. IsNullOrEmpty (this. Text box 1. Text))

{

Meteorological service. WeatherWebServiceSoapClient service = new WeatherService。 WeatherWebServiceSoapClient();

string[]strweather info = service . getweatherbycityname(this。 Text box 1. Text);

StringBuilder str = new StringBuilder(" ");

Strait. AppendLine ("You can view the weather information in the following way:");

Foreach (string information in strwetherinfo)

{

Strait. AppendLine(info+" & lt; br/>);

}

This. Literal 1。 Text =str。 ToString();

}

}

Run the program and you will see the effect.