Problem with using a port number in FtpWebRequest.Create string

vb.net , WebRequest , FTP Stoke-on-Trent, United Kingdom
  • 8 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 = ftp://1.2.3.4/e:/CDR/*.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 = ftp://1.2.3.4:8021/e:/CDR/*.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 vb.net 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 :-)

  • 8 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("ftp://1.2.3.4:8021/e:/CDR/File.tmp") 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).

Contribute

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.

“In theory, theory and practice are the same. In practice, they're not.”