static void Main() { int refresh = int.Parse(ConfigurationManager.AppSettings.Get("RefreshRate")) * 1000; CurrencyConsumer api = new CurrencyConsumer( ConfigurationManager.AppSettings.Get("Url")); Console.WriteLine("Exchange rate USD/GTQ from http://www.banguat.gob.gt/"); while (true) { // Obtain the TipoCambio CurrencyEntry newCurrency = TipoCambio.TipoCambioDia(); if (newCurrency != null) { // Post to the API api.PostCurrencyAsync(newCurrency); } else { Console.WriteLine(String.Format( "TipoCambio Web Service is not available retrying in {0} seconds", refresh / 1000)); } Thread.Sleep(refresh); } }
/// <summary>method <c>TipoCambioDia</c> /// Consumes the TipoCambioDia action from the TipoCambio WebService.</summary> public static CurrencyEntry TipoCambioDia() { CurrencyEntry currencyEntry = null; const string soapBody = @"<TipoCambioDia xmlns=""http://www.banguat.gob.gt/variables/ws/"" />"; XmlDocument soapEnvelopeXml = CreateSoapEnvelope(soapBody); HttpWebRequest webRequest = CreateWebRequest(wsUrl, wsAction); InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); // async call to web request. IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); asyncResult.AsyncWaitHandle.WaitOne(); // Response from the completed web request. string soapResult; using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) { using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) { soapResult = rd.ReadToEnd(); } // Retrieve information from the XML response XmlDocument response = new XmlDocument(); response.LoadXml(soapResult); currencyEntry = new CurrencyEntry(); string fecha = response.GetElementsByTagName("fecha")[0].InnerText; currencyEntry.date = DateTime.ParseExact(fecha, "dd/MM/yyyy", null); currencyEntry.rate = Convert.ToDouble(response.GetElementsByTagName("referencia")[0].InnerText); } return(currencyEntry); }
public async void PostCurrencyAsync(CurrencyEntry record) { // Build query string string queryString = String.Format(endPoint, record.date, record.rate); try { HttpResponseMessage response = await client.PostAsync(queryString, null); Console.WriteLine( String.Format(logStatus, (int)response.StatusCode, response.StatusCode)); } catch (HttpRequestException e) { Console.WriteLine("Error: " + e.Message.ToString()); } }