2

I connect with FTP using following code.

 // Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mydomain.com/websitefolder/downloadfiles/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;


// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("myuser", "********");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Now, I want to set port number but I don't get any property to set port number. Is it possible to set port number using FtpWebRequest? If so, can anybody suggest me?

If it is not possible to set port number. which port number use by FtpWebRequest to connect with FTP?

Updated: I am talking about port number 21 and 22. Please guide.

Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • 2
    Possible duplicate of [Setting port in FtpWebRequest](http://stackoverflow.com/questions/4024922/setting-port-in-ftpwebrequest) – kiziu Sep 16 '16 at 10:21

1 Answers1

4

I think the port can be part of the Request URI itself.

Try changing the first line to this, replace 1201 with port number you want to use:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mydomain.com:1201/websitefolder/downloadfiles/");
sachin
  • 2,341
  • 12
  • 24
  • Sorry but why did you used 1201? Can I use 21 or 22? Please explain me so I will take care. – Nanji Mange Sep 16 '16 at 10:36
  • 21 is a default port for FTP control. Also, sachin clearly wrote "replace 1201 with port number you want to use". – kiziu Sep 16 '16 at 10:38
  • Yes, that was just an example. You may use any valid port that the FTP server listens to. – sachin Sep 16 '16 at 10:38
  • @kiziu According to you, if I don't set any port(just use URL as I have used in question), it will use port 21. Right? – Nanji Mange Sep 16 '16 at 10:44
  • @NanjiMange yes, but bear in mind: http://stackoverflow.com/questions/876712/set-port-number-when-using-ftpwebrequest-in-c-sharp – kiziu Sep 16 '16 at 10:45
  • @kiziu Thank you. I wanted to clear because now a days I am getting a serious issue. I am working on Windows Application. Above code(in question) is working fine in most 80% networks. But in some of networks, I am getting this error : "The underlying connection was closed: The server committed a protocol violation". I still not found any solution. so I thought I should try by setting port. Please suggest if you have any idea. – Nanji Mange Sep 16 '16 at 10:53
  • @NanjiMange have you tried searching SO for your error and checking if any solution applies? I can see a few questions with your error. Or maybe it is a case of network configuration and firewall blocking ports used. – kiziu Sep 16 '16 at 10:56
  • @kiziu I have done enough R & D for this as it stopped my new release. It doesn't raised in 80% of users. This is raised in 20% users. I can apply alternate options if this won't work. – Nanji Mange Sep 16 '16 at 11:02