下载文件抛出内部服务器错误

本文关键字:服务器 错误 内部 文件 下载 | 更新日期: 2023-09-27 18:14:29

此代码抛出异常。如何解决这个问题?

string uriString = "http://www.fileserve.com/login.php";
NameValueCollection postvals = new NameValueCollection();
postvals.Add("loginUserName", "aaaa");
postvals.Add("loginUserPassword", "xxxx");
postvals.Add("autoLogin", "on");
postvals.Add("loginFormSubmit", "Login");
WebClient myWebClient = new WebClient();
myWebClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7");
myWebClient.UploadValues(uriString, "POST", postvals);
string cookiestr = myWebClient.ResponseHeaders.Get("Set-Cookie");
myWebClient.Headers.Add("Cookie", cookiestr);
myWebClient.DownloadFile(new Uri("http://fileserve.com/file/2jkYtZS"), "C:''temp''" + Path.GetFileName("http://www.fileserve.com/file/2jkYtZS/some.avi"));

异常被myWebClient.DownloadFile()方法抛出:

 The remote server returned an error: (500) Internal Server Error.
      at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at SimonDownloader2.Program.Main(String[] args) in C:'Users'Simon'Workspace'C#'Projects'SerialDownloader2'SerialDownloader2'Program.cs:line 32
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

下载文件抛出内部服务器错误

修复服务器上的代码(如果是你的),或者使用你想正确使用的任何代码。

这不是DownloadFile的错误,而是您连接的服务器返回错误HTTP 500。所以我猜你尝试了服务员不喜欢的东西。也许你不应该登录之前,至少获得页面或有一个适当的cookie设置或其他。

最简单的调试方法可能是使用Fiddler,在浏览器中做你正在做的事情,然后观察交互,然后尝试找出你在代码中做错了什么

你不能,这是fileserve.com方面的问题,除非你也运行该服务器,否则你将无法修复它。

现在,这可能是由于您在请求中发送的一些意外参数而发生的。我会首先在namevalue集合中发送更少的值,直到您确定哪个是罪魁祸首。