私はこのコードを使用しています:
クラスのコンストラクターで:
this.wc.Credentials = CredentialCache.DefaultCredentials;
this.wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
private void button1_Click(object sender, EventArgs e)
{
this.wc.DownloadStringAsync(new Uri("http://url" + this.a[0] + ".aspx"));
}
public void wc_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
string result = (string)e.Result;
// do something
this.a.RemoveAt(0);
if (this.a.Count > 0)
{
Console.WriteLine(this.a[0]);
// keep retrieving
}
}
else
{
// display/handle error
}
}
DownloadStringAsync
の結果を変数に設定できないことは理解していますが、ハンドラーでvoid
実行しようとすると、空白が返されます。DownloadStringCompletedEvent
これを他のクラスに分離しようとしていましたが、async/void または何らかの理由でリクエストから何も返すことができません。
メインアプリケーションクラスで使用されるクラスに上記がありますが、http リクエストの結果をメインクラス内の変数に設定できません。何か案は?