在 C# 中使用 FtpWebRequest 时设置端口号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/876712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:06:09  来源:igfitidea点击:

Set Port number when using FtpWebRequest in C#

c#ftpwebrequest

提问by The real napster

I keep getting a exception when I try to FTP to my Win 2008 Server from C# code using VS2008 as debugger.

当我尝试使用 VS2008 作为调试器从 C# 代码 FTP 到我的 Win 2008 服务器时,我一直收到异常。

My test class looks like this:

我的测试类如下所示:

public class FTP
{
    private string ftpServerIP = "192.168.10.35:21";
    private string ftpUserID = "Administrator";
    private string ftpPassword = "XXXXXXXX";
    private string uploadToFolder = "uploadtest";

    public void Upload(string filename)
    {
        FileInfo fileInf = new FileInfo(filename);
        string uri = "ftp://" + ftpServerIP + "/" + uploadToFolder + "/" + fileInf.Name;
        FtpWebRequest reqFTP;

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        reqFTP.UseBinary = true;
        reqFTP.ContentLength = fileInf.Length;

        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;

        FileStream fs = fileInf.OpenRead();
        try
        {
            Stream strm = reqFTP.GetRequestStream();
            contentLen = fs.Read(buff, 0, buffLength);

            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}

When I execute the code I get a Connection Failed with FTP error 227 in the GetRequestStream() call. In the exception I can see the connection fails to: 192.168.10.35:52184

当我执行代码时,我在 GetRequestStream() 调用中收到连接失败,FTP 错误 227。在异常中,我可以看到连接失败:192.168.10.35:52184

I have no idea how it comes up with port 52184. I specify in the ftpServerIP that it should be port 21.

我不知道端口 52184 是如何产生的。我在 ftpServerIP 中指定它应该是端口 21。

I have found a few persons with the same issues on google but I haven't found a good example on how this is solved and I still don't understand why it happens.

我在谷歌上发现了一些有同样问题的人,但我没有找到一个很好的例子来说明如何解决这个问题,我仍然不明白为什么会发生这种情况。

Anyone know how to handle this issue??

有谁知道怎么处理这个问题??

UPDATE:

更新:

I have tried to connect to a different FTP account and there it all works fine. Therefore I tested my 192.168.10.35:21 FTP but it works fine in CuteFTP Pro and the likes. This just makes it even more strange..

我尝试连接到不同的 FTP 帐户,一切正常。因此我测试了我的 192.168.10.35:21 FTP,但它在CuteFTP Pro 等中运行良好。这只会让它更奇怪..

采纳答案by Thies

My guess would be Windows firewall issues, FTP uses other ports than just port 21 - sometimes changing the FTP mode from active to passive helps to get things working.

我的猜测是 Windows 防火墙问题,FTP 使用其他端口,而不仅仅是端口 21 - 有时将 FTP 模式从主动更改为被动有助于使事情正常工作。

reqFTP.UsePassive = false;

Look at this good article on FTP: Active FTP vs. Passive FTP, a Definitive Explanation

看看这篇关于 FTP 的好文章:Active FTP vs. Passive FTP, a Definitive Explanation

回答by The real napster

Thies got it right, it had to do with passive mode

Thies 说得对,它与被动模式有关

The fix in the code is so insanely simple :)

代码中的修复非常简单:)

reqFTP.UsePassive = false;

And it worked fast and without errors!

它工作得很快而且没有错误!

回答by Guillaume.Nepveu

It is important to differentiate the COMMAND port and the DATA port. The connection protocol will also change depending if you are in ACTIVE or PASSIVE mode.

区分 COMMAND 端口和 DATA 端口很重要。连接协议也将根据您处于 ACTIVE 还是 PASSIVE 模式而改变。

ACTIVE MODE :

活动模式:

1) The client initiate a connection from a random unspecified COMMAND port (N > 1023) to the default server COMMAND port (21). The client will specify his DATA port (N+1) and start listening on this port.

1) 客户端从一个随机的未指定 COMMAND 端口(N > 1023)到默认服务器 COMMAND 端口(21)发起连接。客户端将指定他的 DATA 端口 (N+1) 并开始侦听此端口。

2) The server initiate a connection from his default DATA port (20) to specified client DATA port (N+1).

2)服务器从他的默认DATA端口(20)到指定的客户端DATA端口(N+1)发起连接。

PASSIVE MODE :

被动模式:

1) The client initiate a connection from a random unspecified COMMAND port (N > 1023) to the default server COMMAND port (21) with the PASSIVE command. The server open a random DATA port (P > 1023) and send it to the client.

1) 客户端使用 PASSIVE 命令发起从随机未指定 COMMAND 端口 (N > 1023) 到默认服务器 COMMAND 端口 (21) 的连接。服务器打开一个随机的DATA端口(P>1023)并发送给客户端。

2) The client initiate a connection from his DATA port (N+1) to the specified server DATA port (P > 1023).

2)客户端发起从他的DATA端口(N+1)到指定服务器DATA端口(P>1023)的连接。

If you use ACTIVE mode, you will most likely need to let your client's firewall accept the connection from the server to your port (N+1 > 1024).

如果使用 ACTIVE 模式,则很可能需要让客户端的防火墙接受从服务器到端口的连接 (N+1 > 1024)。

In your example, you were in ACTIVE mode. Your client initiated a connection from his COMMAND port (52183) to the server's default COMMAND port (21) and specified its DATA port (52184 = 52183 + 1). Then the server initiated a connection from its default DATA port (20) to the client's DATA port (52184) which was most likely rejected by the client's firewall.

在您的示例中,您处于 ACTIVE 模式。您的客户端启动了从其 COMMAND 端口 (52183) 到服务器默认 COMMAND 端口 (21) 的连接,并指定了其 DATA 端口 (52184 = 52183 + 1)。然后服务器启动了从其默认数据端口 (20) 到客户端的数据端口 (52184) 的连接,该端口很可能被客户端的防火墙拒绝。

I hope this helps you solve your problem!

我希望这可以帮助您解决您的问题!