我想寫一個C#程序來上傳文件到FTP通過代理。C# - 通過HTTP代理上傳文件到FTP
這是我寫的代碼:
public new bool Upload(string localFilePath, string pathUpload)
{
Stream FStream = null;
bool retval = false;
FileStream FlStream;
try
{
FtpWebRequest FtpRequest =
(FtpWebRequest) FtpWebRequest.Create(Uri + pathUpload);
FtpRequest.Credentials = new NetworkCredential(User, Password);
if (ProxyAddress != "" && ProxyAddress != null)
{
WebProxy ftpProxy = new WebProxy();
ftpProxy.Address = new System.Uri(ProxyAddress);
ftpProxy.Credentials =
new System.Net.NetworkCredential(ProxyUserId, ProxyPassword);
FtpRequest.Proxy = ftpProxy;
}
FtpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
FStream = FtpRequest.GetRequestStream();
FileStream fs = File.OpenRead(localFilePath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
FStream.Write(buffer, 0, buffer.Length);
FStream.Close();
FStream.Dispose();
return retval = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.ToString());
return false;
}
}
如果我通過它說,使用HTTP代理時,不支持FTP命令代理地址。
我已經嘗試強制FtpRequest.Proxy = null
建議其他地方(例如http://www.codeproject.com/Questions/332730/FTP-proxy-problem-in-Csharp-application),但它給了我例外「無法連接到遠程服務器」。
我也嘗試使用WebClient
類而不是FtpWebRequest
,但它給了我同樣的問題。
在此先感謝您的幫助。