public static void UploadFile(string filename, string ftpServerIP, string ftpUserID, string ftpPassword,string aPort)
{
FileInfo fileInf = new FileInfo(filename); string uri = "ftp://" + ftpServerIP + ":"+aPort +"/" + fileInf.Name;
FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
reqFTP.UsePassive = true;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
reqFTP.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(RemoteCertificateCallback);
_log.Error("f"); // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb
int buffLength = 2048; byte[] buff = new byte[buffLength];
_log.Error("g"); // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
_log.Error("h"); try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
int contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength);
} // 关闭两个流
strm.Close();
fs.Close();
_log.Info("上传博客例子完全正确");
}
catch (Exception ex)
{
_log.Error(ex);
}
} private static bool RemoteCertificateCallback(Object sender, X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors) {
_log.Debug("证书回调");
try
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true; _log.Error("证书内容:" + sslPolicyErrors);
return false;
}
catch (Exception ee)
{
_log.Error("回调错误:" + ee);
return false;
}
}如上代码:为什这个 函数RemoteCertificateCallback从来都不执行呢。不管是打日志,还是单步调试都不到这个函数这里来,
谁能告诉我??
{
FileInfo fileInf = new FileInfo(filename); string uri = "ftp://" + ftpServerIP + ":"+aPort +"/" + fileInf.Name;
FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
reqFTP.UsePassive = true;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
reqFTP.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(RemoteCertificateCallback);
_log.Error("f"); // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb
int buffLength = 2048; byte[] buff = new byte[buffLength];
_log.Error("g"); // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
_log.Error("h"); try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
int contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength);
} // 关闭两个流
strm.Close();
fs.Close();
_log.Info("上传博客例子完全正确");
}
catch (Exception ex)
{
_log.Error(ex);
}
} private static bool RemoteCertificateCallback(Object sender, X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors) {
_log.Debug("证书回调");
try
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true; _log.Error("证书内容:" + sslPolicyErrors);
return false;
}
catch (Exception ee)
{
_log.Error("回调错误:" + ee);
return false;
}
}如上代码:为什这个 函数RemoteCertificateCallback从来都不执行呢。不管是打日志,还是单步调试都不到这个函数这里来,
谁能告诉我??
我也不懂,没用过。帮你顶。ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateCallback);
和 FtpWebRequest 没有一点联系,是不是少了点什么。
http://codeguru.earthweb.com/csharp/.net/net_security/authentication/article.php/c15051/#more
new RemoteCertificateValidationCallback(RemoteCertificateCallback);
在"="前加个"+"操作,先订阅回调
new RemoteCertificateValidationCallback(RemoteCertificateCallback);
在"="前加个"+"操作,先订阅回调同意!没用订阅事件,而且RemoteCertificateCallback方法返回bool不是很理解。
一般事件中的方法是直接执行的。bool的话在UploadFile中貌似没有判断!