我已经制作了一个小测试应用程序(WinForms C#)来测试FTP上传。这非常有效。
我尝试在我正在使用的Windows服务中使用完全相同的方法,但我得到'(426)连接已关闭;转移中止。' - 消息。
我已经多次确定该方法的参数完全相同。他们是!下一个想法是我的服务运行的帐户,但我已经尝试了所有可能性,甚至以“用户”身份运行服务,提供我自己的凭据。然后它应该像WinForms应用程序,对吧?不,它没有!
它正好运行到using (var requestStream = request.GetRequestStream())
行,这是失败点。
有问题的FTP服务器只允许活动连接,因此request.UsePassive设置为false。
任何人都有线索?
public void UploadToFtp(string url, string filePath, string username, string password, bool mode)
{
var fileName = Path.GetFileName(filePath);
var request = (FtpWebRequest)WebRequest.Create(url + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = !mode;
request.UseBinary = true;
request.KeepAlive = false;
using (var fileStream = File.OpenRead(filePath))
{
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
requestStream.Close();
}
}
var response = (FtpWebResponse)request.GetResponse();
response.Close();
}
添加两种方案的跟踪日志:
WebRequest::Create(ftp://someurl/somefile.txt)
FtpWebRequest#63289421::.ctor(ftp://someurl/somefile.txt)
Exiting WebRequest::Create() -> FtpWebRequest#63289421
Current OS installation type is 'Client'.
RAS supported: True
ServicePoint#14173886::ServicePoint(someurl:21)
FtpWebRequest#63289421::GetRequestStream()
FtpWebRequest#63289421::GetRequestStream(Method=STOR.)
FtpControlStream#22525719 - Created connection from 10.10.10.103:1865 to nnn.nnn.nnn.nnn:21.
Associating FtpWebRequest#63289421 with FtpControlStream#22525719
FtpControlStream#22525719 - Received response [xxxx
someurl>PROD server
Port21>Use active mode>
xxxx]
Sending command [USER myusername]
Received response [331 Enter password]
Sending command [PASS ********]
Received response [230-User logged in
Hi,I'am datagear PROD.
230 User logged in]
Sending command [OPTS utf8 on]
Received response [200 Command OPTS succeed]
Sending command [PWD]
Received response [257 "/CitData" is current directory]
Sending command [TYPE I]
Received response [200 Transfer mode set to BINARY]
Sending command [PORT 10,10,10,103,7,74]
Received response [200 Command PORT succeed]
Sending command [STOR somefile.txt]
Received response [150 Uploading in BINARY file somefile.txt]
Exiting FtpWebRequest#63289421::GetRequestStream()
Received response [226 Transfer completed]
Sending command [QUIT]
Received response [221-bye
Bye-Bye,see you again.
WebRequest::Create(ftp://someurl/somefile.txt)
FtpWebRequest#63289421::.ctor(ftp://someurl/somefile.txt)
Exiting WebRequest::Create() -> FtpWebRequest#25425822
Current OS installation type is 'Client'.
ServicePoint#31665793::ServicePoint(someurl:21)
FtpWebRequest#25425822::GetRequestStream()
FtpWebRequest#25425822::GetRequestStream(Method=STOR.)
FtpControlStream#51484875 - Created connection from 10.10.10.103:1759 to nnn.nnn.nnn.nnn:21.
Associating FtpWebRequest#25425822 with FtpControlStream#51484875
FtpControlStream#51484875 - Received response [xxxx
someurl>PROD server
Port21>Use active mode>
xxxx]
Sending command [USER myusername]
Received response [331 Enter password]
Sending command [PASS ********]
Received response [230-User logged in
Hi,I'am datagear PROD.
230 User logged in]
Sending command [OPTS utf8 on]
Received response [200 Command OPTS succeed]
Sending command [PWD]
Received response [257 "/CitData" is current directory]
Sending command [TYPE I]
Received response [200 Transfer mode set to BINARY]
Sending command [PORT 10,10,10,103,6,224]
Received response [200 Command PORT succeed]
Sending command [STOR somefile.txt]
Received response [426 Transfer failed]
(Releasing FTP connection#51484875.)
GetRequestStream - The remote server returned an error: (426) Connection closed; transfer aborted..
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
Exiting FtpWebRequest#25425822::GetRequestStream()
有效的那个有一行说'RAS Supported'。也许有趣,不知道。
答案 0 :(得分:1)
Windows防火墙导致了这个问题。从WinService运行时,我必须打开此服务的防火墙。当从VS环境运行时,似乎防火墙已经为VS打开了(尽管WinFirewall中的传递应用程序列表没有显示),因此一切似乎运行良好。