Announcement

Collapse
No announcement yet.

FTP Fileupload mit Proxy

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • FTP Fileupload mit Proxy

    Hallo,
    ich bin momentan damit beschäfftigt ein kleines Tool zu schreiben mit dem von einem lokalen Rechner, der hinter einem Proxy sitzt (Ken), Daten von einem FTP herunter- und hochgeladen werden können.

    Der download funktioniert auch einwandfrei

    Public Sub GetFileFromFTP()
    Const BUFFERSIZE = 64 * 1024
    Dim strURL As String
    Dim strURI As String
    Dim strLocalFilePathAndName As String
    Dim FTPrequest As FtpWebRequest
    Dim FTPresponse As FtpWebResponse
    Dim FTPproxy As WebProxy
    Dim FileStream As IO.FileStream
    Dim BinaryReader As IO.BinaryReader
    Dim BinaryWriter As IO.BinaryWriter
    Dim bytaBuffer(BUFFERSIZE - 1) As Byte
    Dim intBufferLoop As Integer

    strURL = fCombinePath(fCombinePath(info.strFTPserver, info.strFTPpath), info.strFTPfile)
    strURI = fGenerateURI()
    strLocalFilePathAndName = fCombinePath(info.strLocalPath, info.strLocalFile)
    FTPproxy = New WebProxy(info.strProxy, info.intProxyPort)
    FTPrequest = FtpWebRequest.Create("ftp://" & info.strFTPserver.Substring(4) & "/" & info.strFTPfile)
    FTPrequest.Proxy = FTPproxy
    FTPrequest.Credentials = New NetworkCredential(info.strFTPuser, info.strFTPpassword)
    FTPresponse = FTPrequest.GetResponse()
    FileStream = New IO.FileStream(strLocalFilePathAndName, IO.FileMode.Create)
    BinaryReader = New IO.BinaryReader(FTPresponse.GetResponseStream)
    BinaryWriter = New IO.BinaryWriter(FileStream)

    Do
    intBufferLoop = BinaryReader.Read(bytaBuffer, 0, BUFFERSIZE)
    BinaryWriter.Write(bytaBuffer, 0, intBufferLoop)
    Dim nfoFile As New FileInfo(strLocalFilePathAndName)

    Form1.StatusStrip1.Items(0).Text = CType(nfoFile.Length / 1024 / 1024, Integer) & " MByte heruntergeladen"
    Threading.Thread.Sleep(5)
    Form1.StatusStrip1.Update()
    Loop Until intBufferLoop = 0

    BinaryReader.Close()
    BinaryWriter.Close()
    FileStream.Close()
    End Sub


    Wenn ich jetzt aber einen Upload machen möchte mit folgendem Code erhalte ich Eine Fehlermeldung ("Der angeforderte FTP-Befehl wird nicht unterstützt, wenn ein HTTP-Proxy verwendet wird.")

    Public Sub Upload()
    Dim requestStream As IO.Stream = Nothing
    Dim fileStream As IO.FileStream = Nothing
    Dim uploadResponse As FtpWebResponse = Nothing
    Dim FTPproxy As WebProxy
    Try
    FTPproxy = New WebProxy(info.strProxy, info.intProxyPort)
    Dim uploadRequest As WebRequest = WebRequest.Create("ftp://" & info.strFTPserver.Substring(4) & "/" & info.strFTPfile)
    uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
    uploadRequest.Credentials = New NetworkCredential(info.strFTPuser, info.strFTPpassword)

    uploadRequest.Proxy = FTPproxy

    requestStream = uploadRequest.GetRequestStream()
    fileStream = IO.File.Open(fCombinePath(info.strLocalPath, info.strLocalFile), IO.FileMode.Open)

    Dim buffer(1024) As Byte
    Dim bytesRead As Integer
    While True
    bytesRead = fileStream.Read(buffer, 0, buffer.Length)
    If bytesRead = 0 Then
    Exit While
    End If
    requestStream.Write(buffer, 0, bytesRead)
    End While

    requestStream.Close()

    uploadResponse = uploadRequest.GetResponse()
    Form1.StatusStrip1.Items(0).Text = "Upload complete."
    Catch ex As UriFormatException
    MsgBox(ex.Message)
    Catch ex As IO.IOException
    MsgBox(ex.Message)
    Catch ex As WebException
    MsgBox(ex.Message)
    Finally
    If uploadResponse IsNot Nothing Then
    uploadResponse.Close()
    End If
    If fileStream IsNot Nothing Then
    fileStream.Close()
    End If
    If requestStream IsNot Nothing Then
    requestStream.Close()
    End If
    End Try
    End Sub


    Da ich schon seit einigen Wochen immer wieder nach Lösungen für dieses Problem suche, bin ich auch schon auf Fragen in diese Richtung gestoßen. Da wird als mögliche Lösung vorgeschlagen uploadRequest.Proxy = Nothing zu setzen. Aber ich nehme an, das hat nur Sinn, wenn man auch keinen Proxy hat.
    Aber da ich mich gerne überraschen lasse habe ich es Trotzdem probiert. Das Ergebnis war eine andere Fehlermeldung: "Die Verbindung mit dem Remoteserver kann nicht hergestellt werden"


    Nun würde ich mich wirklich freuen, wenn mit jemand von euch bei diesem Problem weiterhelfen könnte. Irgendwie muss das doch gehen!


    Gruß,
    Athlone

  • #2
    Also so wie ich das sehe sagt MSDN klar, dass es eben nicht möglich ist über einen WebProxy ein File hochzuladen:

    If the Proxy property is set, either directly or in a configuration file, communications with the FTP server are made through the specified proxy. If the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported.

    Comment


    • #3
      Zu diesem Schluss bin ich in diesem Kontext auch schon gekommen.
      Allerdings muss es doch eine Möglichkeit gebn das umzusetzen. Schließlich gibt es Programme zu kaufen, welche in VB.net programmiert wurden und dieses Feature unterstützen.

      Da ich auf keine anderen Möglichkeiten inder VB.net-Sprache gestoßen bin, habe ich nun angefangen mich über die Windows-API zu informieren und bin da auf die wininet.dll gestoßen, in der es auch sämmtliche ftp-Funktionen gibt. Allerdings bekomme ich diese auch nicht zum laufen.
      Dort treten dann immer Fehlermeldungen auf wie: "Es wurde versucht, im geschützten Speicher zu lesen oder zu schreiben. Dies ist häufig ein Hinweis darauf, dass anderer Speicher beschädigt ist."
      Nun gehe ich davon aus, dass dabei dann etwas mit den Übergebenen Parametern die die Dateipfade beschreiben nicht stimmig ist, allerdings weiß ich nicht genau was.
      Hier der zugehörige Code:
      Public Declare Function fInternetConnect Lib "wininet.dll" Alias _
      "InternetConnectA" (ByVal hInternetSession As Long, _
      ByVal sServerName As String, ByVal nServerPort As Integer, _
      ByVal sUsername As String, ByVal sPassword As String, _
      ByVal lService As Long, ByVal lFlags As Long, ByVal _
      lContext As Long) As Long

      Public Declare Function fInternetOpen Lib "wininet.dll" Alias _
      "InternetOpenA" (ByVal sAgent As String, ByVal lAccessStructure _
      As Long, ByVal sProxyName As String, ByVal sProxyBypass _
      As String, ByVal lFlags As Long) As Long

      Public Declare Function fFtpPutFile Lib "wininet.dll" Alias _
      "FtpPutFileA" (ByVal hFtpSession As Long, ByVal _
      lpszLocalFile As String, ByVal lpszRemoteFile As String, _
      ByVal dwFlags As Long, ByVal dwContext As Long) As Long

      Public Function fPutFile(ByVal strvLocalFilePathAndName As String, ByVal strvFTPFileName As String) As Boolean
      Dim nFlag As Long
      Dim Proxy As String
      Try
      '### Für den Proxybetrieb
      If info.strProxy <> Nothing Then
      Proxy = info.strProxy & ":" & info.intProxyPort
      hOpen = fInternetOpen(FTP_UAgent, INTERNET_OPEN_Type_PROXY, _
      Proxy, vbNullString, 0)
      Else

      '### Ohne Proxy
      hOpen = fInternetOpen(FTP_UAgent, INTERNET_OPEN_Type_DIRECT, _
      vbNullString, vbNullString, 0)
      End If

      If hOpen <> 0 Then
      hConnection = fInternetConnect(hOpen, info.strFTPserver, _
      INTERNET_INVALID_PORT_NUMBER, _
      info.strFTPuser, info.strFTPpassword, _
      INTERNET_SERVICE_FTP, nFlag, 0)
      End If
      Dim Result As Long
      ' upload the file
      If strvLocalFilePathAndName <> Nothing Then
      If strvFTPFileName <> Nothing Then
      Result = fFtpPutFile(hConnection, strvLocalFilePathAndName, "\" & strvFTPFileName, transfer, 0)
      Else
      Result = fFtpPutFile(hConnection, strvLocalFilePathAndName, "\" & fSplitByDelimiter(strvLocalFilePathAndName, "/"c), transfer, 0)
      End If

      If Result = 0 Then MsgBox("Übertragungsfehler!")
      End If
      Catch ex As Exception
      MsgBox(ex.Message)
      End Try

      End Function


      Hierbei habe ich zwar auch schon Codebeispiele gefunden, allerdings fehlen mir dazu dann die exemplarischen übergabeparameter.
      Also wenn jemand einen funktionierenden Code mit Beispielparametern zur Verfügung haben sollte, würde ich mich sehr freuen, ihn mir mal ansehen zu dürfen.

      Ich bin für jeden Tip bei beiden Ansätzen dankbar.

      Gruß,
      Athlone

      Comment

      Working...
      X