gpt4 book ai didi

c# - 从 WinService 上传 FTP 失败

转载 作者:可可西里 更新时间:2023-11-01 09:33:11 26 4
gpt4 key购买 nike

我制作了一个小型测试应用程序 (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();
}

添加两种场景的跟踪日志:

使用 Windows 应用程序:

 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.

使用 Windows 服务:

 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”。也许有趣,不知道。

最佳答案

Windows 防火墙导致了问题。从 WinService 运行时,我必须为此服务打开防火墙。当从 VS 环境运行时,防火墙似乎已经为 VS 打开(尽管 WinFirewall 中的直通应用程序列表没有显示)因此一切似乎都运行良好。

关于c# - 从 WinService 上传 FTP 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35340611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com