Herunterladen von Dateien Mit FtpWebRequest

Ich versuche, eine Datei herunterzuladen, mithilfe von FtpWebRequest.

private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
    int bytesRead = 0;
    byte[] buffer = new byte[1024];

    FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
    request.Method = WebRequestMethods.Ftp.DownloadFile;

    Stream reader = request.GetResponse().GetResponseStream();
    BinaryWriter writer = new BinaryWriter(File.Open(localDestinationFilePath, FileMode.CreateNew));

    while (true)
    {
        bytesRead = reader.Read(buffer, 0, buffer.Length);

        if (bytesRead == 0)
            break;

        writer.Write(buffer, 0, bytesRead);
    }        
}

Verwendet es diese CreateFtpWebRequest Methode, die ich erstellt:

private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive = false)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));

    //Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
    request.Proxy = null;

    request.UsePassive = true;
    request.UseBinary = true;
    request.KeepAlive = keepAlive;

    request.Credentials = new NetworkCredential(userName, password);

    return request;
}

Er downloadet es. Aber die information ist immer beschädigt. Wer weiß, was Los ist???

  • Was meinst du mit "ist immer korrupt"? Bitte genauer sein.
  • Ich hatte zwar ein anderes problem, die Beschreibung Bemerkung über die request.Proxy = null Linie der CreateFtpWebRequest Funktion das problem, dass ich hatte. Daher upvote!
InformationsquelleAutor Rick Eyre | 2012-09-20
Schreibe einen Kommentar