【问题标题】:How to let the WebClient_DownloadStringCompleted handler return a value?如何让 WebClient_DownloadStringCompleted 处理程序返回一个值?
【发布时间】:2013-11-13 18:52:54
【问题描述】:

我有一个从 API 获取一些数据(字符串)的 WebClient。现在,为了我的代码整洁,我想把它放在一个公共字符串而不是 void 中。

字符串值 = GetToTheWebClient(参数);

我怎样才能做到这一点? DownloadStringCompleted 事件处理程序无法返回任何内容。

亲切的问候, 尼尔斯

编辑

我添加了一段代码,可以更好地解释它。

string value = GetToTheWebClient(parameter);

public string GetToTheWebClient(parameter)
{
    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client     _DownloadStringCompleted);
    client .DownloadStringAsync(new Uri("http://ThatsACoolURLBro" + parameter, UriKind.Absolute));
}

void client _DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
     // Do some fancy XML processing.


     >>>> Return the outcome value back to the GetToTheWebClient - string <<<<
}

【问题讨论】:

标签: c# silverlight webclient


【解决方案1】:

我已经玩过 Webclient 类了。请看下面的例子。如果您使用它,请使用它

对这个方法很满意。

public string DownloadWebPageData(string url)
    {
        string html = string.Empty;
        try
        {
            using (ConfigurableWebClient client = new ConfigurableWebClient())
            {
                /* Set timeout for webclient */
                client.Timeout = 600000;

                /* Build url */
                Uri innUri = null;
                if (!url.StartsWith("http://"))
                    url = "http://" + url;

                if (url.EndsWith("."))
                    innUri = ManipulateBrokenUrl(url);
                else
                    Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);

                try
                {
                    client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)");
                    client.Headers.Add("Vary", "Accept-Encoding");

                    // Specify the encoding for unicode characters
                    client.Encoding = Encoding.UTF8;

                    html = client.DownloadString(innUri);

                    if (string.IsNullOrEmpty(html))
                    {
                        return string.Empty;
                    }
                    else
                    {
                        return html;
                    }

                }
                catch (WebException we)
                {
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "";
                }
                finally
                {
                    client.Dispose();
                }
            }
        }
        catch (Exception ex)
        {
            return "";
        }
        return null;
    }

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2021-07-31
    • 2017-12-26
    • 1970-01-01
    • 2016-08-14
    • 2021-08-08
    • 1970-01-01
    • 2011-03-30
    • 2018-07-18
    • 2021-10-15
    相关资源
    最近更新 更多