I try to establish a FTP connection using the FtpWebRequest
. I need to establish the connection through a squid proxy server, that requires me to send a CONNECT
command first.
我尝试使用FtpWebRequest建立FTP连接。我需要通过squid代理服务器建立连接,这需要我先发送CONNECT命令。
I have tried the code below, but this results in a GET
request to the proxy server, which it refuses.
我已经尝试了下面的代码,但是这导致了对代理服务器的GET请求,它拒绝了。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" +server);
request.Credentials = new NetworkCredential(user, pwd);
var webproxy = new WebProxy(new Uri("http://" + proxy+ ":" + proxyPort));
webproxy.UseDefaultCredentials = true;
request.Proxy = webproxy;
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Is there any way that I can use the FtpWebRequest
to establish my connection through the proxy?
有什么方法可以使用FtpWebRequest通过代理建立我的连接?
If not, do you know a good and hopefully free .NET FTP client that I can use? Thank you very much for your help.
如果没有,你知道一个好的,希望免费的.NET FTP客户端,我可以使用吗?非常感谢您的帮助。
2 个解决方案
#1
0
FtpWebRequest is a "session-less" client, so it doesn't allow you to interact with the remote end while connected. I examined the code of FtpWebRequest.cs and I don't see how you can get it to send a "CONNECT" command.
FtpWebRequest是一个“无会话”客户端,因此它不允许您在连接时与远程端进行交互。我检查了FtpWebRequest.cs的代码,我不知道如何让它发送“CONNECT”命令。
"CONNECT" command doesn't seem to be part of the standard FTP command set, so I can't wrap my brain how this fits into something that an FTP library should do.
“CONNECT”命令似乎不是标准FTP命令集的一部分,所以我无法将我的大脑如何适应FTP库应该做的事情。
A good free FTP client that is session-oriented that I use is "SSH.NET Library" at http://sshnet.codeplex.com/ but I believe it only supports sftp. FtpWebRequest supports ftps and ftp. Find out if your FTP server supports sftp and you might be able to use SSH.NET . If your only option is ftps, I can report that I spent a long time looking for a free .net ftps client and I came away using FtpWebRequest.
我使用的面向会话的一个好的免费FTP客户端是http://sshnet.codeplex.com/上的“SSH.NET Library”,但我相信它只支持sftp。 FtpWebRequest支持ftps和ftp。了解您的FTP服务器是否支持sftp,您可以使用SSH.NET。如果你唯一的选择是ftps,我可以报告我花了很长时间寻找一个免费的.net ftps客户端,然后我就使用了FtpWebRequest。
#2
0
FtpWebRequest
supports HTTP proxy (and CONNECT
command is mandatory part of that support). Your code is correct.
FtpWebRequest支持HTTP代理(并且CONNECT命令是该支持的必需部分)。你的代码是正确的。
Though the HTTP proxy is not supported uploads (it is for all other operations). So maybe your actual problem is the upload, while you do not mention it.
虽然HTTP代理不支持上传(它适用于所有其他操作)。所以也许你的实际问题是上传,而你没有提到它。
For FTP uploads through HTTP proxy you have to use a 3rd party FTP client.
对于通过HTTP代理进行FTP上传,您必须使用第三方FTP客户端。
For example with WinSCP .NET assembly, you can use:
例如,使用WinSCP .NET程序集,您可以使用:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
// Configure proxy
sessionOptions.AddRawSettings("ProxyMethod", "3"); // HTTP proxy
sessionOptions.AddRawSettings("ProxyHost", "proxy");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload file
string localFilePath = @"C:\path\file.txt";
string pathUpload = "/file.txt";
session.PutFiles(localFilePath, pathUpload).Check();
}
For the options for the SessionOptions.AddRawSettings
, see raw settings.
有关SessionOptions.AddRawSettings的选项,请参阅原始设置。
Easier is to have WinSCP GUI generate C# FTP code template for you.
更容易让WinSCP GUI为您生成C#FTP代码模板。
Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.
请注意,WinSCP .NET程序集不是本机.NET库。它相当于控制台应用程序上的瘦.NET包装器。
(I'm the author of WinSCP)
(我是WinSCP的作者)
#1
0
FtpWebRequest is a "session-less" client, so it doesn't allow you to interact with the remote end while connected. I examined the code of FtpWebRequest.cs and I don't see how you can get it to send a "CONNECT" command.
FtpWebRequest是一个“无会话”客户端,因此它不允许您在连接时与远程端进行交互。我检查了FtpWebRequest.cs的代码,我不知道如何让它发送“CONNECT”命令。
"CONNECT" command doesn't seem to be part of the standard FTP command set, so I can't wrap my brain how this fits into something that an FTP library should do.
“CONNECT”命令似乎不是标准FTP命令集的一部分,所以我无法将我的大脑如何适应FTP库应该做的事情。
A good free FTP client that is session-oriented that I use is "SSH.NET Library" at http://sshnet.codeplex.com/ but I believe it only supports sftp. FtpWebRequest supports ftps and ftp. Find out if your FTP server supports sftp and you might be able to use SSH.NET . If your only option is ftps, I can report that I spent a long time looking for a free .net ftps client and I came away using FtpWebRequest.
我使用的面向会话的一个好的免费FTP客户端是http://sshnet.codeplex.com/上的“SSH.NET Library”,但我相信它只支持sftp。 FtpWebRequest支持ftps和ftp。了解您的FTP服务器是否支持sftp,您可以使用SSH.NET。如果你唯一的选择是ftps,我可以报告我花了很长时间寻找一个免费的.net ftps客户端,然后我就使用了FtpWebRequest。
#2
0
FtpWebRequest
supports HTTP proxy (and CONNECT
command is mandatory part of that support). Your code is correct.
FtpWebRequest支持HTTP代理(并且CONNECT命令是该支持的必需部分)。你的代码是正确的。
Though the HTTP proxy is not supported uploads (it is for all other operations). So maybe your actual problem is the upload, while you do not mention it.
虽然HTTP代理不支持上传(它适用于所有其他操作)。所以也许你的实际问题是上传,而你没有提到它。
For FTP uploads through HTTP proxy you have to use a 3rd party FTP client.
对于通过HTTP代理进行FTP上传,您必须使用第三方FTP客户端。
For example with WinSCP .NET assembly, you can use:
例如,使用WinSCP .NET程序集,您可以使用:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
// Configure proxy
sessionOptions.AddRawSettings("ProxyMethod", "3"); // HTTP proxy
sessionOptions.AddRawSettings("ProxyHost", "proxy");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload file
string localFilePath = @"C:\path\file.txt";
string pathUpload = "/file.txt";
session.PutFiles(localFilePath, pathUpload).Check();
}
For the options for the SessionOptions.AddRawSettings
, see raw settings.
有关SessionOptions.AddRawSettings的选项,请参阅原始设置。
Easier is to have WinSCP GUI generate C# FTP code template for you.
更容易让WinSCP GUI为您生成C#FTP代码模板。
Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.
请注意,WinSCP .NET程序集不是本机.NET库。它相当于控制台应用程序上的瘦.NET包装器。
(I'm the author of WinSCP)
(我是WinSCP的作者)