[C#] FtpWebRequest fails
I try to list files using FtpWebRequest but it fails with a WebException
'The underlying connection was closed: An unexpected error occurred on a receive.'
string ftpPath = "ftp://" + this._ip;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Credentials = new NetworkCredential(_user, _password);
request.Proxy = null;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream(); >> Exception!!!
|
After trial and error, I found I need to set Active Mode for connection
(default Passive property is false)
string ftpPath = "ftp://" + this._ip;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Credentials = new NetworkCredential(_user, _password);
request.Proxy = null;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
REF:
留言
張貼留言