连接中断时,C#如何捕获和爆发WebClient.DownloadString?

问题描述

我有以下代码可从URL下载XML文件。如果在下载过程中连接断开,则代码将挂起。我该如何打破(或超时)呢? xmlLocation是XML文件的URL。

public static string GetXml(string xmlLocation)
{
    string xmlText = null;

    try
    {
        
        using (var webClient = new WebClient())
        {
            xmlText = webClient.DownloadString(xmlLocation);
        }
    }
    catch (Exception)
    {
        return null;
    }
    
    return xmlText;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)