简体   繁体   中英

Asp .net Ftp UploadFile Request Error 501 PASV not allowed

I can create folder on ftp but when I try upload a file to ftp, it gives me "501 error: PASV not allowed" that is status description. My code is here;

     try
     {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + isim + "/" + upc.PostedFile.FileName);
            request.Credentials = new NetworkCredential("username", "password");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;
            // Copy the contents of the file to the request stream.  
            StreamReader sourceStream = new StreamReader("D:\\KTK\\" + upc.PostedFile.FileName);
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            request.Timeout = System.Threading.Timeout.Infinite;
            
            FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //this  row gives error
            Stream ftpStream = response.GetResponseStream();
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

      }
      catch (WebException ex)
      {
                FtpWebResponse response = (FtpWebResponse)ex.Response;
                var sonuc = response.StatusCode;

      }

            

Simple/Quick Answer

For some reason the FTP server is in active mode. You can connect and run commands, but if you try to transfer data, you must specify the transfer port.

You could try:

request.UsePassive = false;

But I think that wouldn't work for you. Unless you're not using NAT and you're not behind a firewall that's denying incoming connections.

What could you do?

  • The FTP server should run in passive mode (almost everyone does that these days)
  • Your client runs on a standalone computer (no NAT )

Explanation:

Active mode

In active mode, the server asks the client to listen on a port, and the server connects to the client. The server is listening on the command port, but for transmission, the server is trying to connect to the client. That's why it's called active mode .

Passive mode

In passive mode, the server listens on both ports and tells the client where the transfer port is. The server is just listening (waiting for a connection from the client). That's why it's called passive mode .

Why do most FTP servers run in passive mode?

Because today almost every client sits behind a router and uses NAT . The client can open a port for data transmission, but almost every router will refuse the connection.


Further information (If you are curious):

NAT

NAT stands for: Network Address Translation If you understand how NAT works, you will understand why NAT and data transfer with an active FTP server outside of your network will not work. RFC 2663 gives you the reason why NAT exists:

The need for IP Address translation arises when a network's internal IP addresses cannot be used outside the network either because they are invalid for use outside, or because the internal addressing must be kept private from the external network.

How does it work?

  1. Your client sends packets to your router to connect to a server outside of your network. (destination is the router)
  2. Your router changes IP address and port (destination) with the IP address and port number from the server outside your network.
  3. The router stores both addresses and port numbers in a NAT table.
  4. The router sends the packets to the FTP server
  5. The FTP server sends packets to your router
  6. The router looks up its NAT table
  7. If it matches the outgoing packets, it swaps the address and port number and sends the incoming packets to the FTP client.

Why doesn't active mode works with NAT?

No match possible. The router doesn't know which ports your client is listening on. Your router will simply discard the packets from the FTP server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM