473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Problems with FtpWebRequest.GetRequestStream() - C#

Hello,

I am trying to upload a file to a ftp site.
I have my code pasted below.
Expand|Select|Wrap|Line Numbers
  1. FtpWebRequest reqFTP;
  2.  
  3. reqFTP = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri(@"ftp://<IPAddress>/<FolderName>/" + fileInf.Name));
  4.  
  5. reqFTP.Credentials = new NetworkCredential(@"username", "password");
  6. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  7. reqFTP.ContentLength = fileInf.Length;
  8.  
  9. int buffLength = 2048;
  10. byte[] buff = new byte[2048];
  11. int contentLen;
  12. FileStream fs = fileInf.OpenRead();
  13.  
  14. ***Stream strm = reqFTP.GetRequestStream();
  15.  
  16. contentLen = fs.Read(buff, 0, buffLength);
  17.  
  18. while (contentLen != 0)
  19. {
  20.             strm.Write(buff, 0, contentLen);
  21.             contentLen = fs.Read(buff, 0, buffLength);
  22. }
  23.  
  24. strm.Close();
  25. fs.Close();
  26.  
*** 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
Apr 8 '08 #1
10 30944
Plater
7,872 Expert 4TB
EDIT:
Ok I am waaay confused now.
Apr 8 '08 #2
Edit: hold on ...
Well most of the forums I tried used the GetRequestStream()
also the explanation to it is - Retreives the stream used to upload data to an FTP server.
Apr 8 '08 #3
Plater
7,872 Expert 4TB
Does this version of the function work for you?
Expand|Select|Wrap|Line Numbers
  1. public static bool AppendFileOnServer(string fileName, Uri serverUri)
  2. {
  3.     // The URI described by serverUri should use the ftp:// scheme.
  4.     // It contains the name of the file on the server.
  5.     // Example: ftp://contoso.com/someFile.txt. 
  6.     // The fileName parameter identifies the file containing 
  7.     // the data to be appended to the file on the server.
  8.  
  9.     if (serverUri.Scheme != Uri.UriSchemeFtp)
  10.     {
  11.         return false;
  12.     }
  13.     // Get the object used to communicate with the server.
  14.     FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
  15.     request.Method = WebRequestMethods.Ftp.AppendFile;
  16.  
  17.     StreamReader sourceStream = new StreamReader(fileName);
  18.     byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  19.     sourceStream.Close();
  20.     request.ContentLength = fileContents.Length;
  21.  
  22.     // This example assumes the FTP site uses anonymous logon.
  23.     request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
  24.     Stream requestStream = request.GetRequestStream();
  25.     requestStream.Write(fileContents, 0, fileContents.Length);
  26.     requestStream.Close();
  27.     FtpWebResponse response = (FtpWebResponse) request.GetResponse();
  28.  
  29.     Console.WriteLine("Append status: {0}",response.StatusDescription);
  30.  
  31.     response.Close();  
  32.     return true;
  33. }
  34.  
  35.  
Apr 8 '08 #4
Does this version of the function work for you?
Expand|Select|Wrap|Line Numbers
  1. public static bool AppendFileOnServer(string fileName, Uri serverUri)
  2. {
  3.     // The URI described by serverUri should use the ftp:// scheme.
  4.     // It contains the name of the file on the server.
  5.     // Example: ftp://contoso.com/someFile.txt. 
  6.     // The fileName parameter identifies the file containing 
  7.     // the data to be appended to the file on the server.
  8.  
  9.     if (serverUri.Scheme != Uri.UriSchemeFtp)
  10.     {
  11.         return false;
  12.     }
  13.     // Get the object used to communicate with the server.
  14.     FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
  15.     request.Method = WebRequestMethods.Ftp.AppendFile;
  16.  
  17.     StreamReader sourceStream = new StreamReader(fileName);
  18.     byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  19.     sourceStream.Close();
  20.     request.ContentLength = fileContents.Length;
  21.  
  22.     // This example assumes the FTP site uses anonymous logon.
  23.     request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
  24.     Stream requestStream = request.GetRequestStream();
  25.     requestStream.Write(fileContents, 0, fileContents.Length);
  26.     requestStream.Close();
  27.     FtpWebResponse response = (FtpWebResponse) request.GetResponse();
  28.  
  29.     Console.WriteLine("Append status: {0}",response.StatusDescription);
  30.  
  31.     response.Close();  
  32.     return true;
  33. }
  34.  
  35.  
Cannot use this function as there is no file on the server and I cant create one manually.
Apr 8 '08 #5
Plater
7,872 Expert 4TB
well, ammend the file reading portion to use an uploaded file then?
Apr 8 '08 #6
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.GetRequestStream();

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

Could this be because of some firewall?
Apr 8 '08 #7
Plater
7,872 Expert 4TB
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?
Apr 8 '08 #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?
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
Apr 8 '08 #9
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.UsePassive = 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).
Apr 9 '08 #10
This error occurs when you don't have proper READ/WRITE Permissions on FTP Site. Please do check your Permissions.
Nov 12 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SevDer | last post by:
Hi, I am trying to write a simple application which copies the predefined files (hardcoded in the code) to my ftp server. I couldn't make it working. I don't want to use any 3rd party...
0
by: Kevin Spencer | last post by:
Hi all, I am working on a service that uploads METAR weather information to the National Weather Service FTP site. The service I'm authoring is hosted on a Windows 200 server, and the NWS FTP...
0
by: Alexis | last post by:
Hello, I am trying to upload a file to an ftp server using the FtpWebRequest class, but I get the following error. The requested URI is invalid for this FTP command. I do can upload the file...
2
by: jlacefie | last post by:
Hello, I am having trouble with the following code using ASP.Net 2.0. I recieve an error on the line File.OpenRead(fPath) stating could not find file . The fPath comes from a textbox control...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: blearyeye | last post by:
The following code is giving me an error 503, with the message "The remote server returned an error: (503) Bad sequence of commands." The StatusDescription is "503 You are already logged in!\r\n". ...
1
by: =?Utf-8?B?ZGF2ZWg0Mg==?= | last post by:
Hi, We want to upload a file to a business partner using FTP with SSL from our intranet. Our proof of concept solution in VB uses FtpWebRequest based on an example at...
0
by: jason7069 | last post by:
Hi There. I recently implemented a FTP upload feature within an ASP.NET application. I use the standard steps that are posted throughout the web. The basic funcationality works as a file is...
3
by: kpg | last post by:
(Multi-post from microsoft.public.dotnet.framework.aspnet.webservices, Please apply all applicable pardons.) Hi all, I have a web service that FTPs data it receives to a third party FTP site....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.