C# 连接到ftps://URL
我试图使用此代码将文件上载到FTP,问题是当语法点击C# 连接到ftps://URL,c#,ssl,ftp,ftpwebrequest,ftps,C#,Ssl,Ftp,Ftpwebrequest,Ftps,我试图使用此代码将文件上载到FTP,问题是当语法点击serverURI.Scheme!=UriSchemeFtp返回false。这是否意味着我的URI地址设置不正确?我知道这是一个有效的地址,我已经使用ftptest.net来验证该站点是否已启动并正在运行。我的语法有什么错误 private void button1_Click(object sender, EventArgs e) { Uri serverUri = new Uri("ftps://afjafaj.org"); str
serverURI.Scheme!=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;
}
ftps://
前缀不是前缀。根据定义,只有ftp://
方案
无论如何,ftps://
仍然被一些软件识别为是指通过TLS/SSL协议的FTP(安全FTP)。这模仿了https://
方案,这是一种基于TLS/SSL的HTTP(https://
是一种标准方案)
尽管.NET framework无法识别ftps://
要通过TLS/SSL连接到显式模式FTP,请将URI更改为FTP://
,并设置为true
(您已经在做的事情)
请注意,
ftps://
前缀通常指通过TLS/SSL的隐式模式FTP。NET framework仅支持显式模式。尽管即使您的URI确实引用了隐式模式,但无论如何,大多数服务器也会支持显式模式。所以这通常不会是个问题。对于显式模式,有时使用ftpes://
。请参阅我的文章以了解两者之间的区别