Fir*_*ren 9 c# cookies asynchronous webclient windows-phone-8.1
使用Silverlight Windows Phone 8.1项目
我正在尝试从网站加载数据.不过,我必须首先在该网站进行身份验证.所以我在网站上发帖子,使用这里的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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我做的WebClient发送POST与Username和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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了什么/我的期望/阻止了什么:
我的第一个想法是让一切都发生异步并使用等待所有请求.所以我做了我的研究,发现WebClient有一个新的async/await实现.
WebClient.DownloadStringTaskAsync但是我在我WebClient的身上找不到这个方法,所以我假设WP8.1 atm没有强制实施.
第二个想法是使用HttpClient.GetStringAsync(URI)我已经使用并支持的方法async/await.正如我所说,我需要一个Cookie满足要求,所以我做了我的研究并找到了这个.但是,我找不到一个HttpClientHandler也没有HttpClient.CookieContainer或相同的属性.
我也尝试等待一个站点完成然后转到下一个,但是tbo我阻止了我的GUI线程,并且不想eventhandlers在单独的线程中编写整体并且我不知道如何有效地这样做
| 归档时间: |
|
| 查看次数: |
407 次 |
| 最近记录: |