none
FtpWebRequest upload problem RRS feed

  • Question

  • Hello

    I have a problem to upload a file to a FTP server(Which is a real hosting). I will describe the scenario:

    1. I use the below code successfully to upload a file to the server in the country Sweden.

    2. I use teamviewer and using a computer in the country Greece where I try the exact same code and I then do receive errors like those 3 different errors at different tries. So different errors do come.

    I have tried to shut down the Firewall completely and allowing ALL ports for incoming/outcoming bounds in Windows 10.

    The username is only letters: onlyletters

    The password looks like this with a #Nabcdef44#

    I can't figure out why this is happening in Greece and not in Sweden. It also happens on 2 different locations in Greece with different routers,internet and computer.

    What could case this problem?

    system.net.webexception the remote server returned an error (530) not logged in

    System.Net.WebException: The operation has timed out. at System.Net.FtpWebRequest.GetRequestStream()

    System.Net.WebException: System error. ---> System.Net.InternalException: System error.
       at System.Net.PooledStream.PrePush(Object expectedOwner)
       at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 

    private void button2_Click(object sender, EventArgs e) { new Thread(() => FtpUploadFile("C:/folder1/testfile.txt", "ftp://www.mydomain.com/domainfolder/Folder123/UploadedTo/testing.txt", "onlyletters", "Nabcdef44#", "vasilis")).Start(); } public void FtpUploadFile(string filename, string to_uri, string user_name, string password, string uploadingby) { try { //Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(to_uri); request.Method = WebRequestMethods.Ftp.UploadFile; request.EnableSsl = false; //Get network credentials. request.Credentials = new NetworkCredential(user_name, password); //Read the file's contents into a byte array. if (File.Exists(filename)) { byte[] bytes = System.IO.File.ReadAllBytes(filename); //Write the bytes into the request stream. request.ContentLength = bytes.Length; using (Stream request_stream = request.GetRequestStream()) { request_stream.Write(bytes, 0, bytes.Length); request_stream.Close(); } MessageBox.Show("Upload Succeeded!"); //This works in Sweden but not in Greece? } } catch (WebException ex) { MessageBox.Show(ex.ToString()); } }



    • Edited by Silvers11 Thursday, August 9, 2018 8:09 PM
    Thursday, August 9, 2018 8:08 PM

Answers

  • I did figure out the problem.

    My hosting service had blocked the IP in Greece because of many failed attempts to login.

    So now it does work as they unblocked the IPs.

    Thank you for your help

    • Marked as answer by Silvers11 Friday, August 10, 2018 6:36 PM
    Friday, August 10, 2018 6:36 PM

All replies

  • Hi Silvers11,

    Thank you for posting here.

    For your question, the code works well in country Sweden. Hence I think the code is right. Maybe the exception is caused by the mode of FTP. What is the mode type of your FTP? Active or Passive? 

    If your FTP mode is Active, please try to change it to Passive. In passive mode FTP the client initiates both connections to the server, solving the problem of firewalls filtering the incoming data port connection to the client from the server.

    You could use FtpWebRequest.UsePassive Property to set the passive mode of FTP. 

    For more details, please refer to the MSDN document.

    https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usepassive(v=vs.110).aspx

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 10, 2018 7:25 AM
    Moderator
  • Wendy

    Thank you for your answer!

    I understand then what you mean with the .UsePassive mode. I tried to set it both to active and false with the exact same webexception. I am stumped and don't know what the problem is. I don't have any idéas to what causes this problem?

    To mention is that I have also tried to use the FTP client FileZilla in Greece with the same problem. That software cant login to the FTP server either and is one of the best FTP clients on the market. So I beleive the code is not a problem. Something else is?

    Now I don't know but the password I have has a # character at the end. I wonder if that can cause any problem from Greece?

    Nabcdef44#

    system.net.webexception the remote server returned an error (530) not logged in

    request.UsePassive = false;
    
    //Also tried
    request.UsePassive = true;



    • Edited by Silvers11 Friday, August 10, 2018 1:14 PM
    Friday, August 10, 2018 1:03 PM
  • I did figure out the problem.

    My hosting service had blocked the IP in Greece because of many failed attempts to login.

    So now it does work as they unblocked the IPs.

    Thank you for your help

    • Marked as answer by Silvers11 Friday, August 10, 2018 6:36 PM
    Friday, August 10, 2018 6:36 PM