12

由于某些防火墙问题,我们需要使用“主动”模式(即不是通过启动PASV命令)进行 FTP。

目前,我们正在使用以下代码:

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
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();
response.Close();

但这似乎默认使用被动模式;我们可以影响它以强制它使用主动模式上传(以与命令行 ftp 客户端相同的方式)?

4

1 回答 1

25

是的,将UsePassive属性设置为 false。

request.UsePassive = false;
于 2011-03-22T16:02:36.070 回答