FtpWebRequest GetRequestStream()gots在第二次调用

时间:2016-12-09 23:08:50

标签: c# file upload ftp ftpwebrequest

目前我正在编写一个特定的程序,其中一个功能是通过ftp协议下载/上传文件。我做了一个上传的方法,但是当我第二次调用它时,我的程序会冻结(100秒后它会显示超时错误)。 方法:

public static void uploadFile(string FTPAddress, string filePath, string username, string password)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + filePath);

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            FileStream stream = File.OpenRead(filePath);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Flush();
            stream.Dispose();

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(buffer, 0, buffer.Length);
                reqStream.Flush();
                reqStream.Dispose();
            }

            request.Abort();

            MessageBox.Show("Uploaded Successfully");
        }
        catch (Exception e)
        {
            if (MessageBox.Show("Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
            {
                Application.Exit();
            }
            else
                FTPTools.uploadFile({calls this function again (it doesn't matter)});
        }
    }

我按下按钮时调用它:

Application.DoEvents();
FTPTools.uploadFile({my ftp address}, {filename}, {login}, {password});

通过使用Visual Studio Debugger,我发现在

时发生了冻结
Stream reqStream = request.GetRequestStream()

呼叫。现在我没有任何想法如何解决它。也许有人会解决我的问题。

2016年11月11日更新: Network trace log

2016年11月12日更新: 今天我几乎没有更新我的代码,但它对我没有帮助:

public static void uploadFile(string FTPAddress, string filePath, string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + filePath);

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
        request.Proxy = null;
        request.Timeout = 5000;
        request.ServicePoint.ConnectionLeaseTimeout = 5000;
        request.ServicePoint.MaxIdleTime = 5000;

        try
        {
            using (FileStream stream = File.OpenRead(filePath))
            {
                byte[] buffer = new byte[stream.Length];

                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(buffer, 0, buffer.Length);
                    reqStream.Close();
                }
                using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse())
                {
                    resp.Close();
                }
                request.Abort();
            }

            MessageBox.Show("Uploaded Successfully");
        }
        catch (Exception e)
        {
            if (MessageBox.Show("Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
            {
                Application.Exit();
            }
            else
                FTPTools.uploadFile({calls this function again (it doesn't matter)});
        }
        finally
        {
            request.Abort();
        }
    }

2016年12月12日更新

今天我发现我的代码异常:当我从if / else语句的方法中调用此方法(上传文件)时,它会在第二次调用时占用时间。但是当我从没有if / else语句的方法调用此方法(上传文件)时,它在每次调用时都能正常工作。我不知道它为什么会发生,但如果没有if / else语句,我就无法调用我的方法。

例如,如果我调用' upl()'方法:

public void upl()
{
    FTPTools.uploadFile(address, fileName, username, password);
}

如果我打电话给' checker()'此代码会在第二次通话时抛出超时异常。方法:

public void upl()
{
    FTPTools.uploadFile(address, fileName, username, password);
}
public void checker()
{
    int a = 0, b = 0;
    if (a == b)
        upl();
}

0 个答案:

没有答案