DownloadFile抛出内部服务器错误

时间:2011-11-14 23:01:52

标签: c#

此代码抛出异常。如何解决?

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()

2 个答案:

答案 0 :(得分:1)

将代码修复到服务器上(如果它是您的)或使用正确尝试在那里使用的任何代码。

DownloadFile这不是错误,但是您连接的服务器会返回错误HTTP 500.所以我猜您尝试了服务器不喜欢的内容。也许你不应该在至少GETting页面或拥有适当的cookie集之前登录。

最简单的调试方法可能是使用Fiddler,在浏览器中执行您正在执行的操作并观看交互,然后尝试找出代码中出错的内容。

答案 1 :(得分:0)

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

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