此代碼拋出異常。如何解決它?DownloadFile拋出內部服務器錯誤
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()
這是一個服務器問題。 – SLaks
「Set-Cookie」響應頭中的數據通常不兼容爲「Cookie」請求頭的數據。 –
你可以隨時啓動Wireshark並記錄流量。例如,是否所有的POST值都上傳了?對MSDN有一個評論,說UploadValues只上傳一個值:http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadvalues(v=VS.100).aspx – kol