このコードは例外をスローします。それを修正するには?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が1つの値だけをアップロードするというコメントがあります。http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadvalues(v=VS.100).aspx – kol