0

私はこのコードを使用しています:

クラスのコンストラクターで:

 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 リクエストの結果をメインクラス内の変数に設定できません。何か案は?

4

1 に答える 1

0

何を見る必要がありましたか?ソースを文字列として表示し、次の方法でこれを確認できます。

public void wc_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
{
    if (!e.Cancelled && e.Error == null)
    {
        MessageBox.Show((string)e.Result);
    }
}
于 2014-07-22T13:22:11.573 に答える