FTP Error RRS feed

  • Question

  • User-1607406849 posted

    I have a requirment to write code to login to an ftp site and upload a specific text file. I have written the following code, but when I run the code, it gives me an error message "Unable to connect to the remote server" I have checked the ftp site and the user id and password. Everything thing is correct, but the code won't work. Please help. Your valuable feedback is greatly appriciated.

      Public Function Upload() As FtpStatusCode
            Dim request As FtpWebRequest = Nothing
            Dim response As FtpWebResponse = Nothing
            Dim sr As StreamReader = Nothing
            Dim sw As StreamWriter = Nothing

            Dim UploadUri As New Uri("")

                ' Check if the URI is and FTP site
                If Not (UploadUri.Scheme = Uri.UriSchemeFtp) Then
                    Throw New ArgumentException("URI is not an FTp site")
                End If

                request = CType(FtpWebRequest.Create(UploadUri), FtpWebRequest)
                request.Method = WebRequestMethods.Ftp.UploadFile
                request.Proxy = Nothing
                request.Credentials = New System.Net.NetworkCredential("codejkl", "ftppass_321*")

                sw = New StreamWriter(request.GetRequestStream, Encoding.UTF8) ' Not to sure if this is right encoding?
                sr = New StreamReader("C:\test\Test.txt")


                response = CType(request.GetResponse, FtpWebResponse)
                Return response.StatusCode
            Catch ex As WebException
                Throw New ApplicationException("FtpUpload Failed: " & ex.Message, ex)
            Catch ex As Exception
                Throw ex
                If response IsNot Nothing Then response.Close()
                If sr IsNot Nothing Then sr.Close()
                If sw IsNot Nothing Then sw.Close()
            End Try
        End Function
    End Class

    Friday, August 4, 2006 2:22 PM

All replies

  • User-619846739 posted

    Hi coolvaas1,

    Did you notice that you left your password in the example?  You may want to double check that you've updated that and don't use it anymore, just in case.  I notice that the IP address is  Did you mean for that to be which is a common non-public IP address?

    You can test for connectivity using the command prompt:

    telnet ipaddress 21

    That will confirm that your computer has FTP access to a particular IP and port.  From skimming your code, it looks good.




    Wednesday, August 9, 2006 1:06 PM
  • User-1607406849 posted

    Hi Scott,
    Thanks for the feed back. Well I did not leave the password out.
    If you look at the line :
    request.Credentials = New System.Net.NetworkCredential("codejkl", "ftppass_321*")
    second parameter is the ftp password.
    Yes the ip address is correct. It is in our local network and I can connect to the ftp site using telnet and web browser.
    I am not sure why it is not working in the code.
    Please help

    Thursday, August 10, 2006 9:57 AM
  • User989702501 posted

    If it works if you ftp manually, then it is something in your code that is not working.
    When you get the error msgs, does IIS FTP log capture the connecting request ? or it didn't reach IIS ftp at all?

    Friday, August 11, 2006 1:44 AM
  • User-619846739 posted

    I meant that you displayed your password to the world.  It sounds like you're ok with that, I just wanted to make sure you didn't do that by mistake.  (It's been done before).

    What happens if you do the telnet test?  I still think that it is probably your IP address. isn't an internal IP address so it will only work for you if your router is purposefully set up that way.  The telnet test will tell you for sure.  The internet subnet to use is 192.168.1.x (notice the 198 -vs- 192)




    Friday, August 11, 2006 11:30 AM
  • User-1607406849 posted

    I checked the IIS log for ftp and couldn't find any. I tried telnet command and it work without any problems. No that password that I typed is not the actual one. I just made that one up. As I said before this ip is in our intranet site. I tried and external ftp site with the same code, but still it doesn't work. I too believe that there is a problem with the code, but I cannot find the problem.

    Again thanks for your replies.

    Monday, August 14, 2006 9:43 AM
  • User-619846739 posted

    I ran your sample code and it was able to connect to an FTP server that I have.  I changed the IP address to an invalid IP address and it failed with the same error that you posted.  So I believe the issue is related to either the IP address or access to the IP address from the web server.

    You haven't acknowledged the difference between 192 and 198.  The 198 in your code sample is obviously wrong and should be changed to 192 which is a normal internal IP address. Are you sure that you've checked that carefully?

    Once you get past the error of the IP address, you should run into a new error which is "The requested URI is invalid for this FTP command".  That is because the StreamWrite shouldn't be used in that manner.  Here is a site that gives some good examples: http://www.codeproject.com/dotnet/dotnetftp.asp


    Monday, August 14, 2006 11:30 AM
  • User989702501 posted
    It sounds like networking issue then, since you are not seeing any log entry in the ftp log. Check if you have any firewall blocking..... also I can't really tell what ftp mode you are in. if you are using passive mode, then make sure the server is able to accept passive mode connection, etc.
    Monday, August 14, 2006 8:35 PM
  • User-1607406849 posted

    I've rechecked the ftp address, you are correct, the address I had was wrong, I have corrected it but still i get the same error. I  do not have a firewall or antivirus installed. I am still stumped with this.

    Thanks for mentioning about the ip.

    Wednesday, August 16, 2006 9:13 AM
  • User-619846739 posted

    Compounded issues are always fun.  So one down, one to go.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    <o:p> </o:p>

    If your error is the error about not being able to connect to the server (the first error you posted), it means that it fails early in the process.  It could be active/passive mode as Bernard suggested.  I believe the property name is "Mode" so you should be able to set request.Mode = "active" or request.Mode="passive" to do the opposite of what it is now.<o:p></o:p>

    <o:p> </o:p>

    Another test besides the telnet test is to use IE with for the address, or to use another FTP client, just to make sure it is working from the web server.<o:p></o:p>

    <o:p> </o:p>



    Wednesday, August 16, 2006 3:56 PM
  • User989702501 posted
    Now, I would suggest you try
    a) use ftp.exe to connect, make sure you able to login and do dir listing
    b) then, configure your program to ftp in active mode, this essentially will work like (a).........

    tell us the result of each, and if error, give us the full error msgs, + server ftp log entries.
    Wednesday, August 16, 2006 11:00 PM
  • User-1607406849 posted
    I tried your suggesstions, but still no luck. I added
    request.UsePassive = True
    just after
    request.Credentials = New System.Net.NetworkCredential("codejkl", "ftppass_321*")
    statement, but still no luck. I can connect to the ftp address using IS, cuteftp and ftp.exe. No problems there.
    I even tried with
    request.UsePassive = False
    still the code doesn't work
    Thursday, August 17, 2006 10:13 AM
  • User-619846739 posted

    Can you post your error message again?  I want to make sure that we're dealing with the expected error.  It sounds like you have everything covered as far as connecting to the ftp server.



    Saturday, August 19, 2006 10:22 AM
  • User989702501 posted

    if error, give us the full error msgs, + server ftp log entries.??? what the error msgs?


    Sunday, August 20, 2006 2:11 AM
  • User-1607406849 posted

    Here is the error that I received

    {"FtpUpload Failed: Unable to connect to the remote server"}
    I do not have permissions to logon to the ftp server. This error occurs even if I try to connect to a remote site

    Monday, August 21, 2006 2:33 PM
  • User989702501 posted

    I guess you need to talk to the ftp hoster and get relevant log entries.... we need to check a) if your connection actually reach the server, if not - where it got stuck! firewall?  b) if it did connected to the ftp server, what's the status error code?

    anyhow from the error msgs - it looks like it's not reaching the ftp server. So we are back to the beginning again. so I would suggest you read all previous thread..... this looks more like networking issue if you confirm that the server ip address is correct.

    Monday, August 21, 2006 8:38 PM
  • User-1607406849 posted

    OWScott and qbernard, thanks for you help in this. I will try to contact the admin to get the logs. But my other concern is the same thing happens when I try to connect to other remote site too. But if I use IE, cuteftp no such problems.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Any ways thanks a million for you suggestions.<o:p></o:p>

    Much appreciated.

    <o:p> </o:p>


    Tuesday, August 22, 2006 2:08 PM
  • User-619846739 posted

    Hi Coolvaas1,

    You're welcome.  I'm with Bernard, it sounds like everything is set up properly for you so it might take some deeper digging with some network sniffer to see where it's failing.  I've tested your code specifically and it works for me, and you've tested IE and cuteftp and it works, so it's some obscure item that is throwing it off.

    Let us know when you get it though, I'm curious what it could be.



    Tuesday, August 22, 2006 6:37 PM