C# 服务器在ftp C上上载时违反了协议#

C# 服务器在ftp C上上载时违反了协议#,c#,file-upload,ftp,C#,File Upload,Ftp,我试图上传文件的ftp与简历,如果互联网连接丢失。如果internet连接丢失,它会恢复文件上载2-3次,但之后会抛出一个异常,表示“服务器违反了协议”。请检查下面给出的代码,如果您发现代码中有任何问题,请帮助我。我知道连接在代码中的某个地方仍然打开,但我无法识别该位置 protected void upload_file(String server_path, string file_path) { if (reqFTP != null) reqFTP.Abort();

我试图上传文件的ftp与简历,如果互联网连接丢失。如果internet连接丢失,它会恢复文件上载2-3次,但之后会抛出一个异常,表示“服务器违反了协议”。请检查下面给出的代码,如果您发现代码中有任何问题,请帮助我。我知道连接在代码中的某个地方仍然打开,但我无法识别该位置

protected void upload_file(String server_path, string file_path)
{
    if (reqFTP != null)
        reqFTP.Abort();
    bool flag = false;
    FileInfo fileInf = new FileInfo(file_path);
    String file_name = fileInf.Name;
    String ftpusername = "aaaa";
    String ftppassword = "Aas123";
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
        reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
        reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
        reqFTP.KeepAlive = false;
        FtpWebResponse response;
        try
        {
            long size = 0;
            using (response = (FtpWebResponse)reqFTP.GetResponse())
            {
                size = response.ContentLength;

            }
            response.Close();
            reqFTP.Abort();
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
            reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
            reqFTP.Method = System.Net.WebRequestMethods.Ftp.AppendFile;
            reqFTP.KeepAlive = false;
            int b = 0;
            System.IO.FileInfo fi = new System.IO.FileInfo(file_path);
            int bytes = 0;
            System.IO.FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
            fs.Seek(size, SeekOrigin.Begin);
            long total_bytes = fi.Length - size;
            byte[] buffer = new byte[8092]; 
            using (System.IO.Stream rs = reqFTP.GetRequestStream())
            {
                while ((bytes = fs.Read(buffer, 0, buffer.Length)) != 0)
                {
                    try
                    {
                        rs.Write(buffer, 0, bytes);
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show(e1.Message);
                        while (!checkForInternetConnection()) ;
                        upload_file(server_path, file_path);
                    }

                }
            }
            fs.Close();

            using (FtpWebResponse uploadResponse = (FtpWebResponse)reqFTP.GetResponse())
            {
                var value = uploadResponse.StatusDescription;
                MessageBox.Show(value);
                uploadResponse.Close();
            }



        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message);
            while (!checkForInternetConnection()) ;
            FtpStatusCode status;
            using (response = (FtpWebResponse)ex.Response)
            {
                status = (FtpStatusCode)response.StatusCode;                        
            }
            if (status == FtpStatusCode.ActionNotTakenFileUnavailable)
            {
                MessageBox.Show("file not found");
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
                    reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
                    reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
                    reqFTP.KeepAlive = false;
                    int b = 0;

                    System.IO.FileInfo fi = new System.IO.FileInfo(file_path);
                    int bytes = 0;
                    System.IO.FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
                    long total_bytes = fi.Length;

                    byte[] buffer = new byte[8092]; 
                    using (System.IO.Stream rs = reqFTP.GetRequestStream())
                    {
                        while ((bytes = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            try
                            {
                                rs.Write(buffer, 0, bytes);
                            }
                            catch
                            {
                                while (!checkForInternetConnection()) ;
                                upload_file(server_path, file_path);
                            }

                        }
                    }

                    fs.Close();
                    using (FtpWebResponse uploadResponse = (FtpWebResponse)reqFTP.GetResponse())
                    {
                        var value = uploadResponse.StatusDescription;
                        MessageBox.Show(value);
                    }
                }
                catch (WebException ex1)
                {
                    MessageBox.Show("catch it");
                    while (!checkForInternetConnection()) ;
                    upload_file(server_path, file_path);

                }
            }
            else
            {
                while (!checkForInternetConnection()) ;
                upload_file(server_path, file_path);
            }


        }

    }
我试过了

    reqFTP.KeepAlive = true

但这不起作用。

我已经测试了您提供的链接,但可能重复,但这不起作用。这就是为什么我发布我自己的问题,因为我的代码中有些不同。如果您在问题中包含您尝试过的内容,这会有所帮助。给我们看看日志@MartinPrikryl请查看该文件,并告诉我是否可以从中找到问题