【问题标题】:I get WebException timeout when I use webclient.DownloadFile after webclient.ResponseHeaders在 webclient.ResponseHeaders 之后使用 webclient.DownloadFile 时出现 WebException 超时
【发布时间】:2023-12-18 14:01:01
【问题描述】:

我正在尝试创建自己的下载管理器。 当一个链接被添加到下载管理器时,我使用一个 webclient 从服务器获取它的信息。像这样

WebClient webClient = new WebClient();
webClient.OpenRead(link);
string filename = webClient.ResponseHeaders["Content-Disposition"];

之后我使用 DownloadFile 下载文件

FileInfo fileInfo = new FileInfo(path);
if (!fileInfo.Exists)
{
    webClient.DownloadFile(link, path);
}

当我这样做时。我得到一个 WebException 超时。 但是,当我删除 webClient.ResponseHeaders 部分时。它永远不会得到超时异常。 我真的需要阅读 Content-Disposition,因为某些链接上没有文件名。 我什至尝试使用不同的网络客户端来下载和获取它的信息,但我得到了相同的结果。

【问题讨论】:

    标签: c# webclient response-headers webexception


    【解决方案1】:

    我找到了另一种获取文件信息的方法来解决这个问题。

    string Name = "";
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(Link);
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    
    for(int i=0; i < myHttpWebResponse.Headers.Count; i++)
    {
        if (myHttpWebResponse.Headers.Keys[i] == "Content-Disposition")
        {
            Name = myHttpWebResponse.Headers[i];
        }
    }
    myHttpWebResponse.Close();
    

    【讨论】:

      最近更新 更多