我编写了一个应用程序,用于从FTP服务器下载XML文件/将其上传到FTP服务器,并使用了FtpWebRequest方法。
现在我必须添加一个SOCKS 5代理,因为否则服务器将无法访问,但无法找到一种方法来实现。
这里没有代理就可以正常下载和上传代码:
public void Download(string LocalFile)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://" + LoginData.Servername + "/" + LoginData.RemoteFile);
ftpRequest.Credentials = new NetworkCredential(LoginData.Username, LoginData.Passwort);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(LocalFile, FileMode.OpenOrCreate);
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
上传:
public void Upload(string localFile)
{
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + LoginData.Servername + "/" + LoginData.RemoteFile);
ftpRequest.Credentials = new NetworkCredential(LoginData.Username, LoginData.Passwort);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
try
{
WebResponse response = ftpRequest.GetResponse();
}
catch (Exception e)
{
throw e;
}
ftpStream = ftpRequest.GetRequestStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Open);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
File.Delete("RSS_Daten.xml");
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
也许有人可以帮助我。谢谢