Problem with using a port number in FtpWebRequest.Create string , WebRequest , FTP Stoke-on-Trent, United Kingdom
  • 10 years ago

    I have a piece of code that downloads a particular file from multiple target sites. A typical connection string looks like this:- host =*.tmp and I run the following code to get the actual filename i want to download

    '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 'Get the TMP filename Dim dirrequest As FtpWebRequest = FtpWebRequest.Create(host) dirrequest.Method = WebRequestMethods.Ftp.ListDirectory dirrequest.UsePassive = False dirrequest.Credentials = New NetworkCredential(username, password)

    Dim dirresponse As WebResponse = dirrequest.GetResponse Dim filenamereader As StreamReader = New StreamReader(dirresponse.GetResponseStream) Dim filename As String = filenamereader.ReadToEnd

    dirresponse.Close() '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    This code works perfectly for all the machines I need to contact. My problem comes from changing the connection string to specify a port number e.g.

    host =*.tmp

    When I run the code the request just throws a timeout exception on GetResponse()

    I tried setting the request to .UsePassive =True and this just caused it to never return from the GetResponseStream.

    I can contact the machine directly from explorer if I cut and paste the connection string in, so i know it isn't a connection problem on my PC.

    This is really bugging me now -enough to actually post on a forum! I've been able to solve every problem up to now reading other peoples postings but this one has beaten me. According to all the documentation it should just work in this format. I must say, though, that although I've been developing for years I'm new to both and FTP so this could be an obvious problem that everyone (but me) knows the answer to - but I'm not proud, feel free to speak down to me :-)

    What am I missing??????

    Thanks in advance for any suggestions :-)

  • 10 years ago

    More Information:-

    Having Hardcoded the filename I tried separating out the various other FTpWebRequests I make in order to get to the download procedure. I found the followng piece of code worked- Dim sizerequest As FtpWebRequest = FtpWebRequest.Create("") sizerequest.Method = WebRequestMethods.Ftp.GetFileSize sizerequest.UsePassive = False sizerequest.Credentials = New NetworkCredential(username, password) Dim dataLength As Integer = sizerequest.GetResponse().ContentLength

    Why would this Method work but the following ones not (They give a timeout exception)? :- WebRequestMethods.Ftp.ListDirectory WebRequestMethods.Ftp.ListDirectoryDetails and WebRequestMethods.Ftp.DownloadFile

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth