我试图上传文件,但是我收到了以下未处理的例外情况。
未处理的类型' System.Net.WebException'发生在System.dll
中
当它到达以下行时会发生错误:
using (var requestStream = request.GetRequestStream())
以下是我的主叫代码:
UploadFileToFtp(@"C:\file.txt", "xyx@abc.com", "foobar");
方法:
public static void UploadFileToFtp(string filePath, string username, string password)
{
var fileName = Path.GetFileName(filePath);
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.alphabeta.com/" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
using (var fileStream = File.OpenRead(filePath))
{
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
requestStream.Close();
}
}
var response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload done: {0}", response.StatusDescription);
response.Close();
}
我可以使用FileZilla和WSFTP使用登录信息访问Web服务器:
FTP / SSL自动检测
以下是例外情况:
System.Net.WebException was unhandled
HResult=-2146233079
Message=System error.
Source=System
StackTrace:
at System.Net.FtpWebRequest.GetRequestStream()
at PredictionEngine.Form1.UploadFileToFtp(String filePath, String username, String password) in C:\Projects\PredictionEngine\PredictionEngine\Form1.cs:line 184
at PredictionEngine.Form1.SendResultsToWeb() in C:\Projects\PredictionEngine\PredictionEngine\Form1.cs:line 157
at PredictionEngine.Form1.SendFileToWebButton_Click(Object sender, EventArgs e) in C:\Projects\PredictionEngine\PredictionEngine\Form1.cs:line 150
at System.Windows.Forms.Control.OnClick(EventArgs e)