【问题标题】:How to set port number using FtpWebRequest in C#?如何在 C# 中使用 FtpWebRequest 设置端口号?
【发布时间】:2016-09-16 10:16:24
【问题描述】:

我使用以下代码连接 FTP。

 // Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mydomain.com/websitefolder/downloadfiles/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;


// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("myuser", "********");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

现在,我想设置端口号,但我没有任何属性来设置端口号。是否可以使用FtpWebRequest 设置端口号?如果是这样,有人可以建议我吗?

如果无法设置端口号。 FtpWebRequest 使用哪个端口号连接 FTP?

更新:我说的是端口号 21 和 22。 请指导。

【问题讨论】:

标签: c# ftp


【解决方案1】:

我认为端口可以是请求 URI 本身的一部分。

尝试将第一行更改为此,将 1201 替换为您要使用的端口号:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mydomain.com:1201/websitefolder/downloadfiles/");

【讨论】:

  • 对不起,您为什么使用 1201?我可以使用 21 或 22 吗?请解释一下,我会注意的。
  • 21 是 FTP 控制的默认端口。此外,sachin 清楚地写了“将 1201 替换为您要使用的端口号”。
  • 是的,这只是一个例子。您可以使用 FTP 服务器侦听的任何有效端口。
  • @kiziu 根据你的说法,如果我不设置任何端口(只使用我在问题中使用的 URL),它将使用端口 21。对吗?
  • @NanjiMange 是的,但请记住:stackoverflow.com/questions/876712/…
猜你喜欢
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 2023-03-15
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多