是否有可能做“主动"操作?使用FtpWebRequest的FTP模式? [英] Is it possible to do "Active" mode FTP using FtpWebRequest?

查看:74
本文介绍了是否有可能做“主动"操作?使用FtpWebRequest的FTP模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Due to some firewall issues, we need to do FTP using "active" mode (i.e. not by initiating a PASV command).

当前,我们使用的代码大致如下:

Currently, we're using code along the lines of:

// 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客户端相同的方式)来强制它上载吗?

But this seems to default to using passive mode; Can we influence this to force it to upload using active mode (in the same way that the command line ftp client does)?

推荐答案

是,设置

Yes, set the UsePassive property to false.

request.UsePassive = false;

这篇关于是否有可能做“主动"操作?使用FtpWebRequest的FTP模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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