Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接到ftps://URL_C#_Ssl_Ftp_Ftpwebrequest_Ftps - Fatal编程技术网

C# 连接到ftps://URL

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

我试图使用此代码将文件上载到FTP,问题是当语法点击
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://
。请参阅我的文章以了解两者之间的区别