using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace HttpClient
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void cmdGetData_Click(object sender, RoutedEventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost/Seattle.xml"));
            request.BeginGetResponse(OnData, request);
        }

        private void OnData(IAsyncResult e)
        {
            HttpWebRequest request = (HttpWebRequest)e.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(e);

            StreamReader data = new StreamReader(response.GetResponseStream());

            // Synchronizace s UI
            Dispatcher.BeginInvoke(() => { txtData.Text = data.ReadToEnd(); });
        }
    }
}
