【问题标题】:How can I retrieve multiple websites asynchronously?如何异步检索多个网站?
【发布时间】:2014-12-02 21:31:46
【问题描述】:

使用 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 发送带有UsernamePasswordPOST 并继续获取我的站点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));
        }
    }
}

我尝试了什么 / 我期望什么 / 是什么阻止了它

  • 我的第一个想法是让一切异步发生,并在所有请求上使用 await。 所以我做了研究,发现WebClient 有一个新的async/await 实现。 WebClient.DownloadStringTaskAsync 但是我在我的WebClient 中找不到这个方法,所以我认为 WP8.1 atm 没有实现。

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

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

【问题讨论】:

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


    【解决方案1】:

    找到了解决办法。

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

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 2021-11-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多