FTP over SSL问题

时间:2013-02-05 19:20:03

标签: c# ssl ftp

我在通过SSL将文件上传到ftp站点到特定目录时遇到问题。我正在使用System.Net.FtpWebRequest class来实现此目的。上传通过罚款。但是文件总是掉到主目录。知道可能做错了什么吗?感谢您的帮助。

    public bool UploadFile(string srcFilePath, string destFilePath = null)
    {
        if (String.IsNullOrWhiteSpace(srcFilePath))
            throw new ArgumentNullException("Source FilePath.");

        if (String.IsNullOrWhiteSpace(destFilePath))
            destFilePath = Path.GetFileName(srcFilePath);

        Uri serverUri = GetUri(destFilePath);

        //// the serverUri should start with the ftp:// scheme.
        if (serverUri.Scheme != Uri.UriSchemeFtp)
            return false;

        // get the object used to communicate with the server.
        FtpWebRequest request = CreateFtpRequest(serverUri, WebRequestMethods.Ftp.UploadFile);

        // read file into byte array
        StreamReader sourceStream = new StreamReader(srcFilePath);
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        // send bytes to server
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Debug.WriteLine("Response status: {0} - {1}", response.StatusCode, response.StatusDescription);

        return true;
    }

    private FtpWebRequest CreateFtpRequest(Uri serverUri, string method)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.EnableSsl = true;
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = true;
        request.Credentials = new NetworkCredential(_userName, _password);
        request.Method = method;
        return request;
    }

    private Uri GetUri(string remoteFilePath)
    {
        return new Uri(_baseUri, remoteFilePath);
    }

1 个答案:

答案 0 :(得分:1)

行。终于想通了。它是.NET 4.0框架问题。使用.NET 3.5构建解决方案,它运行得非常好。

讨厌从微软发布.NET新版本中的漏洞并浪费大量的时间来搞清楚。