DownloadFile上的Weberror System.Net.WebException:远程服务器返回错误:(403)Forbidden。在System.Net.WebClient.DownloadFile

时间:2017-06-09 11:13:57

标签: c#

每当我从网上下载原始mp4时,我都会收到此错误:

Weberror at DownloadFile System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at ClipToTube.Web.DownloadFile(String url) in C:\Path\To\App\Web.cs:line 53

我的代码:

try
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36");

    string fileName = DateTime.Now.Ticks.ToString() + ".mp4";
    string filePath = Path.Combine(Functions.GetAppFolder() + "Clips", fileName);

    wc.DownloadFile(url, filePath);
    Thread.Sleep(500);

    if (File.Exists(filePath))
        return filePath;

    Thread.Sleep(1500);
    break;
}
catch (WebException ex)
{
    Console.WriteLine($"Weberror at DownloadFile {ex}");
}

在此之后,我得到Error: An unhandled exception caused mBackgroundWork to crash.

需要帮助解决它们为什么会发生以及如何解决它们。

0 个答案:

没有答案