How to improve the Performance of FtpWebRequest?

后端 未结 18 1025
忘掉有多难
忘掉有多难 2020-11-28 04:46

I have an application written in .NET 3.5 that uses FTP to upload/download files from a server. The app works fine but there are performance issues:

  1. It take

相关标签:
18条回答
  • 2020-11-28 05:10

    KeepAlive is working. FtpWebRequest caches connections inside, so they can be reused after some time. For details and explanation of this mechanism you can look to ServicePoint.

    Another good source of information is to look into FtpWebRequest source (you can do it on VS2008).

    0 讨论(0)
  • 2020-11-28 05:11

    AFAIK, each FtpWebRequest has to set up a new connection - including logon to the server. If you want to speed up the FTP transfers, I would recommend that you use an alternate FTP client instead. Some of these alternate clients can login and then perform multiple actions using the same command connection.

    Examples of such clients incldue: http://www.codeproject.com/KB/IP/FtpClient.aspx which also includes a good explanation as to why these libraries can operate faster than the standard FtpWebRequest and http://www.codeproject.com/KB/macros/ftp_class_library.aspx which looks like a simple enough implementation also.

    Personally, I rolled my own implementation of FTP back in the .NET 1.1 days before the FtpWebRequest was introduced and this still works well for .NET 2.0 onwards.

    0 讨论(0)
  • 2020-11-28 05:13

    This link describes ConnectionGroupName and KeepAlive affects: WebRequest ConnectionGroupName

    0 讨论(0)
  • 2020-11-28 05:13

    To resolve the problem about performance you simply need to set:

    ftpRequest.ConnectionGroupName = "MyGroupName"; ftpRequest.KeepAlive = false; ftpRequest.ServicePoint.CloseConnectionGroup("MyGroupName");

    0 讨论(0)
  • 2020-11-28 05:13

    Try this below code, you will get better performence:

    private void Upload144_Click(object sender, EventArgs e)
    {
        OpenFileDialog fileobj = new OpenFileDialog();
        fileobj.InitialDirectory = "C:\\";
        //fileobj.Filter = "Video files (*.mp4)";
        //fileobj.ShowDialog();
    
        if (fileobj.ShowDialog() == DialogResult.OK)
        {
            if (fileobj.CheckFileExists)
            {
                string test = Properties.Settings.Default.Connection;
                SqlConnection con = new SqlConnection(test);
                con.Open();
                string correctfilename = System.IO.Path.GetFileName(fileobj.FileName);
                SqlCommand cmd = new SqlCommand("Insert into Path(ID,Path5) VALUES   ((select isnull(MAX(id),0) + 1 from Path),'\\Videos\\" + correctfilename + "')", con);
    
                cmd.ExecuteNonQuery();
    
                string path = Application.StartupPath.Substring(0, Application.StartupPath.Length - 10);
                con.Close();
    
                //For Progressbar
                DataTable dt = new DataTable();
           //   SqlDataAdapter da = new SqlDataAdapter(cmd);
           //   da.Fill(dt);
    
                timer5.Enabled = true;
    
                // FOR FtpServer File Upload::
                string uploadfile = fileobj.FileName;
                string uploadFileName = new FileInfo(uploadfile).Name;
    
                string uploadUrl = "ftp://ftp.infotech.com/";
                FileStream fs = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
                try
                {
                    long FileSize = new FileInfo(uploadfile).Length; // File size of file being uploaded.
                    Byte[] buffer = new Byte[FileSize];
    
                    fs.Read(buffer, 0, buffer.Length);
    
                    fs.Close();
                    fs = null;
                    string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
                    FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
                    requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                    requestObj.Credentials = new NetworkCredential("test@sample.com", "test@123");
                    Stream requestStream = requestObj.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
    
                    requestStream.Flush();
                    requestObj = null;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:16

    Debug Network

    A few tricks for simple network debugging:

    1. Check the response times when you ping the FTP server from the application server.
    2. Check the response times for a trace route (tracert from a DOS shell).
    3. Transfer a file from the command-line using the ftp command.
    4. Connect to the FTP server via Telnet: telnet server 21.

    The results will provide clues to solving the problem.

    Network Hardware

    For a slow trace route:

    • Determine why the two computers are having network issues.
    • Upgrade the network hardware between the slowest link.

    Network Configuration

    For a slow ping:

    • Check the network configuration on each machine.
    • Ensure the settings are optimal.

    Validate API

    A slow command-line FTP session will tell you that the problem is not isolated to the FTP API you are using. It does not eliminate the API as a potential problem, but certainly makes it less likely.

    Network Errors

    If packets are being dropped between the source and destination, ping will tell you. You might have to increase the packet size to 1500 bytes to see any errors.

    FTP Queue Server

    If you have no control over the destination FTP server, have an intermediary server receive uploaded files. The intermediary then sends the files to the remote server at whatever speed it can. This gives the illusion that the files are being sent quickly. However, if the files must exist on the remote server as soon as they are uploaded, then this solution might not be viable.

    FTP Server Software

    Use a different FTP daemon on the FTP server, such as ProFTPd as a Windows service. (ProFTPd has plug-ins for various databases that allow authentication using SQL queries.)

    FTP Server Operating System

    A Unix-based operating system might be a better option than a Microsoft-based one.

    FTP Client Software

    There are a number of different APIs for sending and receiving files via FTP. It might take some work to make your application modular enough that you can simply plug in a new file transfer service. A few different APIs are listed as answers here.

    Alternate Protocol

    If FTP is not an absolute requirement, try:

    • a Windows network drive
    • HTTPS
    • scp, rsync, or similar programs (Cygwin might be required)
    0 讨论(0)
提交回复
热议问题