FtpWebRequest UploadFile返回"The underlying connection was closed: The server committed a protocol violation."解决方法

将FtpWebRequest的KeepAlive设置为true。

return Return<Boolean>(
                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);

 

posted @ 2014-08-06 17:23  junchu25  阅读(1298)  评论(0编辑  收藏  举报