Problems with FtpWebRequest.GetRequestStream() - C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulvachhani
    New Member
    • Apr 2008
    • 6

    Problems with FtpWebRequest.GetRequestStream() - C#

    Hello,

    I am trying to upload a file to a ftp site.
    I have my code pasted below.
    Code:
    FtpWebRequest reqFTP;
    
    reqFTP = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri(@"ftp://<IPAddress>/<FolderName>/" + fileInf.Name));
    
    reqFTP.Credentials = new NetworkCredential(@"username", "password");
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.ContentLength = fileInf.Length;
    
    int buffLength = 2048;
    byte[] buff = new byte[2048];
    int contentLen;
    FileStream fs = fileInf.OpenRead();
    
    ***Stream strm = reqFTP.GetRequestStream();
    
    contentLen = fs.Read(buff, 0, buffLength);
    
    while (contentLen != 0)
    {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
    }
    
    strm.Close();
    fs.Close();
    *** at this point an error pops up
    Message = "The remote server returned an error: (500) Syntax error, command unrecognized."

    Could someone please let me know as to why this is happening?

    Thanks very much.

    Rahul
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    EDIT:
    Ok I am waaay confused now.

    Comment

    • rahulvachhani
      New Member
      • Apr 2008
      • 6

      #3
      Originally posted by Plater
      Edit: hold on ...
      Well most of the forums I tried used the GetRequestStrea m()
      also the explanation to it is - Retreives the stream used to upload data to an FTP server.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Does this version of the function work for you?
        Code:
        public static bool AppendFileOnServer(string fileName, Uri serverUri)
        {
            // The URI described by serverUri should use the ftp:// scheme.
            // It contains the name of the file on the server.
            // Example: ftp://contoso.com/someFile.txt. 
            // The fileName parameter identifies the file containing 
            // the data to be appended to the file on the server.
            
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {
                return false;
            }
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
            request.Method = WebRequestMethods.Ftp.AppendFile;
            
            StreamReader sourceStream = new StreamReader(fileName);
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
         
            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
            FtpWebResponse response = (FtpWebResponse) request.GetResponse();
            
            Console.WriteLine("Append status: {0}",response.StatusDescription);
            
            response.Close();  
            return true;
        }

        Comment

        • rahulvachhani
          New Member
          • Apr 2008
          • 6

          #5
          Originally posted by Plater
          Does this version of the function work for you?
          Code:
          public static bool AppendFileOnServer(string fileName, Uri serverUri)
          {
              // The URI described by serverUri should use the ftp:// scheme.
              // It contains the name of the file on the server.
              // Example: ftp://contoso.com/someFile.txt. 
              // The fileName parameter identifies the file containing 
              // the data to be appended to the file on the server.
              
              if (serverUri.Scheme != Uri.UriSchemeFtp)
              {
                  return false;
              }
              // Get the object used to communicate with the server.
              FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
              request.Method = WebRequestMethods.Ftp.AppendFile;
              
              StreamReader sourceStream = new StreamReader(fileName);
              byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
              sourceStream.Close();
              request.ContentLength = fileContents.Length;
           
              // This example assumes the FTP site uses anonymous logon.
              request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
              Stream requestStream = request.GetRequestStream();
              requestStream.Write(fileContents, 0, fileContents.Length);
              requestStream.Close();
              FtpWebResponse response = (FtpWebResponse) request.GetResponse();
              
              Console.WriteLine("Append status: {0}",response.StatusDescription);
              
              response.Close();  
              return true;
          }
          Cannot use this function as there is no file on the server and I cant create one manually.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            well, ammend the file reading portion to use an uploaded file then?

            Comment

            • rahulvachhani
              New Member
              • Apr 2008
              • 6

              #7
              Originally posted by Plater
              well, ammend the file reading portion to use an uploaded file then?
              Thanks for all your help.

              But the problem here is that - when it reaches

              Stream strm = reqFTP.GetReque stStream();

              The error pops up
              Message = "The remote server returned an error: (500) Syntax error, command unrecognized."

              Could this be because of some firewall?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Possibly? Really it sounds like you are not talking to a real FTP?
                HAve you tried patcket watching with ethereal to see what is being sent?

                Comment

                • rahulvachhani
                  New Member
                  • Apr 2008
                  • 6

                  #9
                  Originally posted by Plater
                  Possibly? Really it sounds like you are not talking to a real FTP?
                  HAve you tried patcket watching with ethereal to see what is being sent?
                  Well Ill explain the situation bit more...

                  I have 2 ftp sites 1 local on my office network and the other is not on my office network.

                  I have this other application in VB which uploads the file onto the server outside my office and it runs fine.

                  When I use this code to upload a file onto the server within the office it runs fine but when I try to run it for the server outside tht error pops up.

                  This is my problem.

                  Thanks,
                  R

                  Comment

                  • rahulvachhani
                    New Member
                    • Apr 2008
                    • 6

                    #10
                    Originally posted by rahulvachhani
                    Well Ill explain the situation bit more...

                    I have 2 ftp sites 1 local on my office network and the other is not on my office network.

                    I have this other application in VB which uploads the file onto the server outside my office and it runs fine.

                    When I use this code to upload a file onto the server within the office it runs fine but when I try to run it for the server outside tht error pops up.

                    This is my problem.

                    Thanks,
                    R

                    Found the problem...
                    Add

                    reqFTP.UsePassi ve = false;

                    UsePassive: Specifies whether to use either active or passive mode. Earlier, active FTP worked fine with all clients, but now, as most of the random ports are blocked by a firewall, the active mode may fail. The passive FTP is helpful in this case. But still, it causes issues at the server. The higher ports requested by client on server may also be blocked by a firewall. But, because FTP servers will need to make their servers accessible to the greatest number of clients, they will almost certainly need to support passive FTP. Passive mode is considered safe because it ensures all data flow initiation comes from inside (client) the network rather than from the outside (server).

                    Comment

                    • zaeem

                      #11
                      This error occurs when you don't have proper READ/WRITE Permissions on FTP Site. Please do check your Permissions.

                      Comment

                      Working...