我的项目有问题。我想使用代理从ftp中删除我的文件。
我的代码是:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FtpServerName + FtpFilePath);
request.Method = WebRequestMethods.Ftp.DeleteFile;
request.Proxy = new WebProxy(ProxyAddress);
request.Proxy.Credentials = new NetworkCredential(ProxyUserName, ProxyPassword);
request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
在这我得到的错误如下: 使用http代理
时,不支持请求的FTP命令任何人都可以帮助我
提前致谢
答案 0 :(得分:1)
来自http://blogs.msdn.com/b/adarshk/archive/2004/09/13/229069.aspx:
关于在FTPWebRequest上使用Http Proxy的注意事项:Http代理仅支持有限数量的ftp方法(主要仅用于下载文件),因此如果您的计算机上有代理的IE设置,则需要明确设置FtpWebRequest不使用代理如下
request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
如果要通过代理执行其他FTP操作,则必须找到另一个支持它的FTP组件。
答案 1 :(得分:0)
而不是request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
尝试request.Proxy = WebRequest.DefaultWebProxy;
遵循适合我的演示代码:
var request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://99.999.99.99/TextFile1.txt"));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("ftp_user", "ftp_pass"); // it's FTP credentials, not proxy
request.Proxy = WebRequest.DefaultWebProxy;
var sourceStream = new StreamReader("TextFile1.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();
var response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();