Row*_*haw 11 c# ftp ftpwebrequest
由于一些防火墙问题,我们需要使用"活动"模式进行FTP(即不通过启动PASV命令).
目前,我们使用的代码如下:
// 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();
Run Code Online (Sandbox Code Playgroud)
但这似乎默认使用被动模式; 我们能否影响它以强制它使用活动模式上传(与命令行ftp客户端相同)?
nos*_*nos 24
是,将UsePassive属性设置为false.
request.UsePassive = false;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10938 次 |
| 最近记录: |