[Mono-bugs] [Bug 478451] New: WebClient.UploadData throws "ProtocolViolationException" on FTP upload

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sat Feb 21 05:46:57 EST 2009


https://bugzilla.novell.com/show_bug.cgi?id=478451


           Summary: WebClient.UploadData throws
                    "ProtocolViolationException" on FTP upload
    Classification: Mono
           Product: Mono: Class Libraries
           Version: SVN
          Platform: 32bit
        OS/Version: Ubuntu
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: System
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: martijndijksterhuis at gmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5)
Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5

Trying to upload an FTP file through the WebClient class results in an
exception:

[System.Net.ProtocolViolationException: Operation is not valid due to the
current state of the object at System.Net.FtpWebRequest.BeginGetRequestStream
(System.AsyncCallback callback, System.Object state) [0x00000]]

I have tried the code included below on Mono SVN (fails), Mono 1.9.1 (fails)
and Visual C# studio 2008 (succeeds) 





Reproducible: Always

Steps to Reproduce:
1.Use the code below to upload a file
2.Watch it fail with the above exception
Actual Results:  
System.Net.WebException: An error occurred performing a WebClient request. --->
System.Net.ProtocolViolationException: Operation is not valid due to the
current state of the object
  at System.Net.FtpWebRequest.BeginGetRequestStream (System.AsyncCallback
callback, System.Object state) [0x00000] 
  at System.Net.FtpWebRequest.GetRequestStream () [0x00000] 
  at System.Net.WebClient.UploadDataCore (System.Uri address, System.String
method, System.Byte[] data, System.Object userToken) [0x00000] 
  at System.Net.WebClient.UploadData (System.Uri address, System.String method,
System.Byte[] data) [0x00000] --- End of inner exception stack trace ---

  at System.Net.WebClient.UploadData (System.Uri address, System.String method,
System.Byte[] data) [0x00000] 
  at System.Net.WebClient.UploadData (System.Uri address, System.Byte[] data)
[0x00000] 
  at (wrapper remoting-invoke-with-check) System.Net.WebClient:UploadData
(System.Uri,byte[])
  at BasicFTPClientNamespace.BasicFTPClient.UploadData (System.String path,
System.Byte[] Data) [0x0001d] in
/home/nis/martijn/Projects/BasicFTP/BasicFTP/Main.cs:83 
  at BasicFTPClientNamespace.BasicFTPClient.UploadFile (System.String ftppath,
System.String srcfile) [0x00056] in
/home/nis/martijn/Projects/BasicFTP/BasicFTP/Main.cs:109 
  at BasicFTPClientNamespace.MainClass.Main (System.String[] args) [0x0002f] in
/home/nis/martijn/Projects/BasicFTP/BasicFTP/Main.cs:127 


Expected Results:  
Successful upload of the file to the FTP server.

// Main.cs created with MonoDevelop
// User: martijn at 4:39 PM 2/18/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Net;
using System.IO;

namespace BasicFTPClientNamespace
{
  class BasicFTPClient
  {
      public string Username;
      public string Password;
      public string Host;
      public int    Port;

      public BasicFTPClient()
      {
          Username = "anonymous";
          Password = "anonymous at internet.com";
          Port     = 21;
          Host     = "";
      }

       public BasicFTPClient(string theUser,string thePassword,string theHost)
      {
          Username = theUser;
          Password = thePassword;
          Host = theHost;
          Port = 21;
      }

      private Uri BuildServerUri(string Path)
      {
          return new Uri(String.Format("ftp://{0}:{1}/{2}",Host,Port,Path));
      }


      public byte[] UploadData(string path,byte[] Data)
      {
          // Get the object used to communicate with the server.
          WebClient request = new WebClient();

          // Logon to the server using username + password
          request.Credentials = new NetworkCredential (Username,Password);
             return request.UploadData(BuildServerUri(path),Data);
      }

      public byte[] UploadFile(string ftppath,string srcfile)
      {
          // Read the data from disk
          FileStream fs = new FileStream(srcfile, FileMode.Open);
          byte [] FileData = new byte[fs.Length];

          int numBytesToRead = (int)fs.Length;
          int numBytesRead = 0;
          while (numBytesToRead > 0)
          {
              // Read may return anything from 0 to numBytesToRead.
              int n = fs.Read(FileData, numBytesRead, numBytesToRead);

              // Break when the end of the file is reached.
              if (n == 0) break;

              numBytesRead += n;
              numBytesToRead -= n;
          }
          numBytesToRead = FileData.Length;
          fs.Close();

          // Upload the data from the buffer
          return UploadData(ftppath,FileData);
      }

        }

  class MainClass
  {
            public static void Main(string[] args)
      {
         BasicFTPClient MyClient = new BasicFTPClient();

         MyClient.Host="myserver";
         MyClient.Port=21;
         MyClient.Username="myusername";
         MyClient.Password="mypassword";

         try
          {
             MyClient.UploadFile("upload.test","/tmp/output.txt");
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }


      }
  }
}

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list