我正在尝试使用此代码将文件上传到FTP,我遇到的问题是,当语法命中serverURI.Scheme != Uri.UriSchemeFtp时,它会返回false。这是否意味着我的URI地址设置不正确?我知道这是一个有效的地址,我已经使用ftptest.net验证了该站点是否已启动并运行。我的语法中有什么地方不正确?
private void button1_Click(object sender, EventArgs e)
{
Uri serverUri = new Uri("ftps://afjafaj.org");
string userName = "Ricard";
string password = "";
string filename = "C:\\Book1.xlsx";
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
UploadFile(serverUri, userName, password, filename);
}
public bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public bool UploadFile(Uri serverUri, string userName, string password, string fileName)
{
if (serverUri.Scheme != Uri.UriSchemeFtp)
return false;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.EnableSsl = true;
request.Credentials = new NetworkCredential(userName, password);
request.Method = WebRequestMethods.Ftp.UploadFile;
StreamReader sourceStream = new StreamReader(fileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Response status: {0}", response.StatusDescription);
}
catch (Exception exc)
{
throw exc;
}
return true;
}发布于 2015-05-07 14:18:01
ftps://前缀不是standard IANA URI scheme。根据RFC 1738的定义,只有ftp://方案。
无论如何,ftps://仍然被一些软件识别为指的是基于TLS/SSL协议的FTP (secure FTP)。这模仿了https://方案,它是一种HTTP over TLS/SSL (https://是一个标准方案)。
尽管.NET框架不能识别ftps://。
要连接到显式模式FTP over TLS/SSL,请将URI更改为ftp://,并将FtpWebRequest.EnableSsl设置为true (您已经在这么做了)。
请注意,ftps://前缀通常指的是隐式模式FTP over TLS/SSL。.NET框架仅支持显式模式。即使您的URI确实指的是隐式模式,但大多数服务器也会支持显式模式。所以这通常不会是一个问题。对于explicit模式,有时会使用ftpes://。要了解FTP over TLS/SSL implicit and explicit modes之间的区别,请参阅我的文章。
https://stackoverflow.com/questions/30082054
复制相似问题