连接到ftps://URL [英] Connect to ftps:// URL

查看:392
本文介绍了连接到ftps://URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码将文件上传到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;
}

解决方案

ftps://前缀不是 RFC 1738 定义.

无论如何,某些软件仍然可以识别ftps://来引用基于TLS/SSL协议的FTP(安全FTP).模仿https://方案,它是基于TLS/SSL的HTTP(https://是标准方案).

.c框架无法识别ftps://.

要通过TLS/SSL连接到显式模式FTP,请将URI更改为ftp://,然后设置基于TLS/SSL的FTP隐式和显式模式之间的区别. /p>

I am trying to use this code to upload a file to an FTP, the problem I have is that when the syntax hits the serverURI.Scheme != Uri.UriSchemeFtp it returns false. Does that mean I have my URI address set-up incorrectly? I know it is a valid address, I have used ftptest.net to verify the site is up and running. What is incorrect in my syntax?

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;
}

解决方案

The ftps:// prefix is not a standard IANA URI scheme. There's only ftp:// scheme, as defined by RFC 1738.

Anyway, the ftps:// is still recognized by some software to refer to an FTP over TLS/SSL protocol (secure FTP). This mimics https:// scheme, which is an HTTP over TLS/SSL (https:// is a standard scheme).

Though the ftps:// is NOT recognized by the .NET framework.

To connect to the explicit mode FTP over TLS/SSL, change your URI to ftp://, and set FtpWebRequest.EnableSsl to true (what you are doing already).


Note that the ftps:// prefix commonly refers to an implicit mode FTP over TLS/SSL. The .NET framework supports only an explicit mode. Though even if your URI is indeed referring to the implicit mode, most servers would support the explicit mode too anyway. So this won't be a problem typically. For the explicit mode, ftpes:// is sometimes used. See my article to understand the difference between FTP over TLS/SSL implicit and explicit modes.

这篇关于连接到ftps://URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆