使用WebClient下载文件时。它第一次下载但第二次下载时发生超时异常。
文件名在标题中contentDisposition
使用webClient.OpenRead(filePath);
然后使用此名称保存。下面显示了使用代码
using (WebClient webClient = new WebClient())
{
try
{
webClient.OpenRead(filePath);
string cpString = webClient.ResponseHeaders["Content-Disposition"];
ContentDisposition contentDisposition = new ContentDisposition(cpString);
string filename = contentDisposition.FileName;
localPath = Path.Combine(fullpath, filename);
if (File.Exists(localPath))
{
int count = 1;
string fileNameOnly = Path.GetFileNameWithoutExtension(localPath);
string extension = Path.GetExtension(localPath);
string path = Path.GetDirectoryName(localPath);
string newlocalPath = localPath;
while (File.Exists(newFullPath))
{
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
newlocalPath= Path.Combine(path, tempFileName + extension);
}
webClient.DownloadFile(filePath, newlocalPath);
}
else webClient.DownloadFile(filePath, localPath);
}
catch (Exception)
{
throw;
}
finally
{
if (webClient != null)
{
webClient.Dispose();
}
}
}
这是一种正确的方法吗?如果您为此提出解决方案,我将不胜感激。