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

New Unity 3D Project

Today I will present a new project that I started. From the post about the car I am building a game with cars, or transportation. The idea is very simple: the player starts with a car and a mission, when he is done with the mission he gets some cash that can be spent buying a new vehicle. Here are some pics: - the car in Unity - the car in Blender - working in the texture - looks great - a wheel - the texture in Gimp - back in Unity testing different materials  like water - and code

Upgrading Windows 10 Home to Pro

 So I have been thinking about upgrading my Windows 10 Home Edition to the Pro version, but I always get to the point where it seems that I had to reinstall the entire SO and quit. After some investigating I have done it this way: - following this post  on the microsoft site I use one of the default keys for Windows 10 Pro and went to Settings > Update & Security > Activation > Change the product key; - next, Windows will activate the Pro functionalities and asks to restart; - now you have the Pro version but it's not activated, so you have to buy a Windows Pro Key. I went to UR cdkeys  and bought a key for less then €20; - and with the new key went to Change the product key and activated; - and it's done. Disclaimer : I have nothing to do with UR cdkeys so you can use any site to buy you cd key and your experience may vary from mine.