FtpWebRequest UploadFile返回"The underlying connection was closed: The server committed a protocol vio...

将FtpWebRequest的KeepAlive设置为true。

return Return(
                new Uri(ftpPath + fileName),
                request =>
                {
                    request.Credentials = credentials;
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request.KeepAlive = true;
                    request.UseBinary = true;

                    using (var requestStream = request.GetRequestStream())
                    {
                        var bufferSize = 8192;
                        var read = 0;
                        var buffer = new Byte[bufferSize];

                        while ((read = stream.Read(buffer, 0, bufferSize)) > 0)
                            requestStream.Write(buffer, 0, read);
                    }
                },
                respone => true);

 

你可能感兴趣的:(c#)