{
FtpWebRequest ftpRequest;
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(“ftp://” + url));
ftpRequest.Credentials = new NetworkCredential(userName, passWord);
ftpRequest.UseBinary = true;
ftpRequest.KeepAlive = true;
}
catch (Exception e)
{
throw e;
}
return ftpRequest;
}
以上没捕捉到错,错在下面
/// <summary>
/// 列出FTP服务器上面当前目录的全部文件和目录
/// </summary>
public FileStruct[] ListFilesAndDirectories(string remoteFatherDirctory)
{
try
{
FtpWebRequest request = GetRequest(remoteFatherDirctory);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader( response.GetResponseStream(), Encoding.Default);
string Datastring = stream.ReadToEnd();
FileStruct[] list = GetList(Datastring);
return list;
}
catch(WebException ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
response 返回的是null,request请求一大堆错误如下节选
ClientCertificates = {System.Security.Cryptography.X509Certificates.X509CertificateCollection}
ConnectionGroupName = null
ContentLength = 0
ContentType = “request.ContentType”引发了“System.NotSupportedException”类型的异常
EnableSsl = false
Headers = {Host: 172.*****:8121
Proxy-Connection: Keep-Alive
Method = “LIST”
PreAuthenticate = “request.PreAuthenticate”引发了“System.NotSupportedException”类型的异常
Proxy = {System.Net.WebRequest.WebProxyWrapper}
ReadWriteTimeout = 300000
Timeout = 100000
UseDefaultCredentials = “request.UseDefaultCredentials”引发了“System.NotSupportedException”类型的异常
}
KeepAlive = true
5
其中的FtpWebRequest 是同一个对象么?
10
5
30
/// <summary> /// 列出FTP服务器上面当前目录的全部文件和目录 /// ListFilesAndDirectories("shop"); /// </summary> public string ListFilesAndDirectories(string remoteFatherDirctory) { try { FtpWebRequest request = GetRequest("ftp://127.0.0.1/" + remoteFatherDirctory, "username", "password"); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string Datastring = stream.ReadToEnd(); return Datastring; } catch (WebException ep) { throw ep; } } private FtpWebRequest GetRequest(string url, string uid, string pwd) { FtpWebRequest ftpRequest; try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); ftpRequest.Credentials = new NetworkCredential(uid, pwd); ftpRequest.UseBinary = true; ftpRequest.KeepAlive = true; } catch (Exception e) { throw e; } return ftpRequest; }