Avançar para o conteúdo principal

Consuming a web service

In this post we will be talking about on how to consume a web service with a desktop application developed with C#.

A web service is an API that is available through the internet and it can be used within the rules established. Some services are free and don't require identification others have to be payed or have some limitations on the number of calls or data produced.

To demonstrate this concept we will be using a free service that the European Central Bank maintains in the following url http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml.
This service returns the Euro currency rate over other countries currencies. This information is encapsulated in XML.

Since we are making a desktop app we have to keep in mind the responsiveness of the user interface (GUI). So the request of the data must not occur in the main thread of the application which could block the GUI during the wait that is associated with exchange of information through the internet.

The main code is this:

async void downloadXML()
        {
            string url = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
            WebRequest pedido = WebRequest.Create(url);
            WebResponse resposta = await pedido.GetResponseAsync();
            XDocument documento = XDocument.Load(resposta.GetResponseStream());
            textBox1.Text = documento.ToString();

            //preencher a listview
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(documento.ToString());
            XmlNodeList lista = xml.GetElementsByTagName("Cube");
            foreach (XmlNode no in lista)
            {
                if (no.Attributes.Count > 1)
                {
                    ListViewItem novo = new ListViewItem(no.Attributes[0].InnerXml);
                    novo.SubItems.Add(no.Attributes[1].InnerXml);
                    listView1.Items.Add(novo);
                }
            }
        }

This function is asynchronous. The execution starts normally and goes until the line where the reserved word AWAIT define a process that can promote a delay so the control is return to the line that call this asynchronous function. When this request terminates the function concludes the processing and updates the UI with the data returned (learn more).

The code that lays ahead is simply to manipulate the XML recived.

The project

Comentários

Mensagens populares deste blogue

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D. The Unity 3D physics engine is used in order to give the car a real behavior. This are the steps: [1] - Create a new Project

PacMan 3D

In this post we will be making a simple and classic game, the Pacman, with Unity 3D. Let's create a new a new project

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't