Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET FTP
Hi i need help with this code to upload one exe to multiple ftp servers.
i have a file with all the links i need and user names and passwords , now it only uploads the first time every on after that gets the error invalid uri even though its correct.
 

-----------------------------------------------------------------------
Imports System.Net
Imports System.IO
Imports System.Data.SqlClient
Public Class Formftp
 
  Private Sub Buttonftp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonftp.Click
    Dim locat, combind, str As String
    locat = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
 
    combind = System.IO.Path.Combine(locat, "FTPstr.cfg")
 
    Dim reader As New StreamReader(combind)
 
    str = reader.ReadToEnd()
 
    Dim TestArray() As String = str.Split(",")
    Dim strftp As String
    Dim s As Boolean
 
    For t As Integer = 0 To TestArray.Length - 1
      strftp = TestArray(t).ToString()
      Dim TestFTPArray() As String = strftp.Split("/")
 

      ' make a reference to a directory
      Dim di As New IO.DirectoryInfo(locat + "\ftp")
      Dim diar1 As IO.FileInfo() = di.GetFiles()
      Dim dra As IO.FileInfo
 
      'list the names of all files in the specified directory

      For Each dra In diar1
        Try
 
          Dim struri As String = String.Format("ftp://{0}/{1}/{2}", TestFTPArray(0), TestFTPArray(1), dra)
 
          Dim clsRequest As System.Net.FtpWebRequest = _
           DirectCast(System.Net.WebRequest.Create(struri), System.Net.FtpWebRequest)
 
          clsRequest.Credentials = New System.Net.NetworkCredential(TestFTPArray(2), TestFTPArray(3))
          clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
 
          ' read in file...
          Dim st As String = String.Format("{0}{1}{2}", locat, "\ftp\", dra)
          Dim bFile() As Byte = System.IO.File.ReadAllBytes(st)
 
          ' upload file...
          Dim clsStream As System.IO.Stream = _
              clsRequest.GetRequestStream()
          clsStream.Write(bFile, 0, bFile.Length)
          clsStream.Close()
          clsStream.Dispose()
          clsRequest.Abort()
 
        Catch ex As Exception
 
        End Try
 
      Next
 

        ' set up request...
    Next
 
  End Sub
End Class
 

update 2012/07/30:
i tested with one exe(trying to put it to multiple servers) it uploads it to the first ftp server then when connecting to the second and every ftp server after it it says "Invalid URI: The hostname could not be parsed." on the (
Dim clsRequest As System.Net.FtpWebRequest = _
           DirectCast(System.Net.WebRequest.Create(struri), System.Net.FtpWebRequest)
) line
even though i can use the exact same destination ftp://ip/folder/ in a browser ect with the username and password as is in the file it is reading and open the ftp folder in the browser. So there is no problem with the ftp folder itself , just the code doesn't want to ftp the exe to a second or third ftp server.
Posted 27-Jul-12 3:55am
Edited 29-Jul-12 23:28pm
v3
Comments
Dan Steuer at 27-Jul-12 10:31am
   
By "uploads the first time every on after that gets the error invalid uri even though its correct" do you mean it uploads the first file correctly but no other files upload or do you mean it uploads all the files to the first server but none of the other servers?
Christian Graus at 27-Jul-12 17:16pm
   
Post your exact error. If it says it is incorrect, it probably is.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should start with some basic debugging so you can ask a more specific question than 'fix my code'.
 
1) run it in a debugger. Trap on each FTP connect.. does it get there twice?
 
2) add some status prints to your code to see what it is trying to do.. how it is parsing your configuration.. etc.
  Permalink  
Comments
dragoncwg at 30-Jul-12 5:34am
   
Thanks never saw as it was reading out of the file it adds a space infront of the server ip. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 510
1 Prakriti Goyal 255
2 Yogesh Kumar Tyagi 224
3 Maciej Los 195
4 CPallini 180
0 OriginalGriff 6,632
1 Sergey Alexandrovich Kryukov 5,429
2 Maciej Los 3,474
3 Peter Leow 3,309
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 30 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100