如何异步检索多个网站?

时间:2014-10-08 09:14:50

标签: c# cookies asynchronous webclient windows-phone-8.1

使用Silverlight Windows Phone 8.1项目

我试图从网站加载数据。不过,我必须首先在该网站进行身份验证。 所以我使用来自here的CookieAwareWebClient的轻微修改版本在网站上发帖。

class CookieAwareWebClient : WebClient
{
    public CookieContainer Cookies = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
            (request as HttpWebRequest).CookieContainer = Cookies;

        return request;
    }
}

现在,我创建一个WebClient,使用POSTUsername发送Password,然后继续获取我的网站async,并在我的DownloadStringCompleted-EventHandler中处理网站数据。 到现在为止还挺好。 现在我想扩展它并获得多个网站。 我没有一次性完成所有这些操作,事实上最好让他们一个接一个地

但我不知道如何去做。

到目前为止我的代码

using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Windows.Web.Http;
using Microsoft.Phone.Shell;
using StackOverflowApp.Resources;

namespace StackOverflowApp
{
    public partial class MainPage
    {
        private const string URL_DATES = @"/subsite/dates";
        private const string URL_RESULTS = @"/subsite/results";

        private readonly ApplicationBarIconButton btn;

        private int runningOps = 0;
        //Regex's to parse websites
        private readonly Regex regexDates = new Regex(AppResources.regexDates);
        private readonly Regex regexResults = new Regex(AppResources.regexResults);

        private readonly CookieAwareWebClient client = new CookieAwareWebClient();
        private int status;

        // Konstruktor
        public MainPage()
        {
            InitializeComponent();

            btn = ((ApplicationBarIconButton)ApplicationBar.Buttons[0]);

            // = application/x-www-form-urlencoded
            client.Headers[HttpRequestHeader.ContentType] = AppResources.ContentType;
            client.UploadStringCompleted += UploadStringCompleted;
            client.DownloadStringCompleted += DownloadStringCompleted;
        }

        private void GetSite()
        {
            const string POST_STRING = "name={0}&password={1}";

            var settings = new AppSettings();

            if (settings.UsernameSetting.Length < 3 || settings.PasswordSetting.Length < 3)
            {   
                MessageBox.Show(
                    (settings.UsernameSetting.Length < 3 
                        ? "Bitte geben Sie in den Einstellungen einen Benutzernamen ein\r\n" : string.Empty) 
                    +
                    (settings.PasswordSetting.Length < 3
                        ? "Bitte geben Sie in den Einstellungen ein Kennwort ein\r\n" : string.Empty)
                );
                return;
            }


            LoadingBar.IsEnabled = true;     
            LoadingBar.Visibility = Visibility.Visible;
            client.UploadStringAsync(
                new Uri(AppResources.BaseAddress + "subsite/login"),
                "POST",
                string.Format(POST_STRING, settings.UsernameSetting, settings.PasswordSetting));
        }

        private void LoadDates()
        {
            status = 0; //Termine   
            runningOps++;

            client.DownloadStringAsync(new Uri(AppResources.BaseAddress + URL_DATES));
        }

        private void LoadResults()
        {
            status = 1; //Ergebnisse      
            runningOps++;
            client.DownloadStringAsync(new Uri(AppResources.BaseAddress + URL_RESULTS));
        }

        private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            runningOps--;
            if (runningOps == 0)
            {
                //alle Operationen sind fertig 
                LoadingBar.IsEnabled = false;
                LoadingBar.Visibility = Visibility.Collapsed;
                btn.IsEnabled = true;
            }
            if (e.Cancelled || e.Error != null) return;

            //Antwort erhalten
            var source = e.Result.Replace("\r", "").Replace("\n", "");
            switch (status)
            {
                case 0: //Termine geladen

                    foreach (Match match in regexDates.Matches(source))
                    {
                        var tb = new TextBlock();
                        var g = match.Groups;

                        tb.Text = string.Format(
                            "{1} {2} {3}{0}{4} {5}{0}{6}",
                            Environment.NewLine,
                            g[1].Value,
                            g[2].Captures.Count > 0 ? g[2].Value : string.Empty,
                            g[3].Captures.Count > 0 ? "- " + g[3].Value : string.Empty,
                            g[5].Value,
                            g[6].Captures.Count > 0 ? "bei " + g[6].Value : string.Empty,
                            (
                                g[7].Captures.Count > 0
                                    ? g[7].Value 
                                    : string.Empty 
                            )
                                + 
                            (
                                g[8].Captures.Count > 0
                                    ? g[8].Value != g[4].Value
                                        ? g[8].Value + " != " + g[4].Value
                                        : g[8].Value
                                    : g[4].Captures.Count > 0
                                        ? g[4].Value
                                        : string.Empty
                            )
                            );

                        DatesPanel.Children.Add(tb);
                    }

                    break;

                case 1: //Ergebnisse geladen
                    foreach (Match match in regexResults.Matches(source))
                    {
                        var tb = new TextBlock();
                        var g = match.Groups;

                        tb.Text = string.Format(
                            "{1} {2} {3}{0}{4} {5}{0}{6}",
                            Environment.NewLine,
                            g[1].Value,
                            g[2].Captures.Count > 0 ? g[2].Value : string.Empty,
                            g[3].Captures.Count > 0 ? "- " + g[3].Value : string.Empty,
                            g[5].Value,
                            g[6].Captures.Count > 0 ? "bei " + g[6].Value : string.Empty,
                            (
                                g[7].Captures.Count > 0
                                    ? g[7].Value 
                                    : string.Empty 
                            )
                                + 
                            (
                                g[8].Captures.Count > 0
                                    ? g[8].Value != g[4].Value
                                        ? g[8].Value + " != " + g[4].Value
                                        : g[8].Value
                                    : g[4].Captures.Count > 0
                                        ? g[4].Value
                                        : string.Empty
                            )
                            );

                        ResultsPanel.Children.Add(tb);
                    }

                    break;
                default:
                    return;
            }
        }

        void UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            //Login completed
            LoadDates();
            //THIS WOULD YIELD AN ERROR FROM THE WEBCLIENT SAYING IT ISNT SUPPORTING MULTIPLE ASYNC ACTIONS
            //LoadResults();
        }

        private async void ClickOnRefresh(object sender, EventArgs e)
        {
            var isUp = await IsUp();
            if (isUp)
                GetSite();
            else                                                         
                MessageBox.Show("Die Seite ist down! :(");

        }

        private void ClickOnSettings(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }

        private async Task<bool> IsUp()
        {
            btn.IsEnabled = false;
            const string ISUPMELINK = "http://www.isup.me/{0}";
            var data = await RequestData(string.Format(ISUPMELINK, AppResources.BaseAddress.Replace("https://", string.Empty)));
            var isUp = !data.Contains("It's not just you!");
            btn.IsEnabled = true;

            return isUp;
        }

        private async void ClickOnTestConnection(object sender, EventArgs e)
        {
            var isUp = await IsUp();
            MessageBox.Show(string.Format("Die Seite ist {0}! :{1}", isUp ? "up" : "down", isUp ? ")" : "("));
        }

        private static async Task<string> RequestData(string url)
        {
            using (var httpClient = new HttpClient())
                return await httpClient.GetStringAsync(new Uri(url));
        }
    }
}

我尝试了什么/我的预期/阻止了什么

  • 我的第一个想法是让一切都发生异步并在所有请求上使用等待。 所以我做了我的研究,发现WebClient有一个新的async/await实现。 WebClient.DownloadStringTaskAsync但是我无法在WebClient中找到此方法,因此我假设WP8.1 atm没有强制执行。

  • 第二个想法是使用我已经使用的HttpClient.GetStringAsync(URI)方法,它支持async/await。 正如我所说,我需要一个Cookie来处理请求,所以我做了我的研究并找到了this。 但是,我找不到HttpClientHandler,也没有HttpClient.CookieContainer或相同的属性。

  • 我也试过等待一个站点完成然后转到下一个站点,但是tbo我阻止了我的GUI线程并且不想在单独的线程中编写整个eventhandlers而且我不知道如何有效地这样做

1 个答案:

答案 0 :(得分:5)

找到解决方案。

导入WebClient.DownloadStringTaskAsync NUGET PACKAGE时,我可以使用Microsoft Async。 这post让我首先想到了nuget。