Link to home
Start Free TrialLog in
Avatar of coperations07
coperations07Flag for United States of America

asked on

FtpWebRequest

Hi,
I'm using VS2010 and .Net Framework 4.0.

I have done quite a bit of work with VB.Net, but just recently decided to work with C# to learn some new things.  We still have a legacy system that I need to communicate with to kick off a job. I've done this in the past with VB.Net using the Shell command and it works quite well. I can't find that the Shell command is available with C#, so I've been trying to use the FtpWebRequest. Can someone help me out here? Below is what works for me in VB.Net and below that is what I'm trying to use for C#. qc_jcl4.txt is the file I'm sending up.

            Using swFTP As StreamWriter = New StreamWriter(psFilePath & "qc_ftp4.txt")
                swFTP.WriteLine("open mfvipa.amgreetings.com")
                swFTP.WriteLine(sLogonId)
                swFTP.WriteLine(sPass)
                swFTP.WriteLine("quote site filetype=jes")
                swFTP.WriteLine("quote site jesl=155")
                swFTP.WriteLine("put " & """" & psFilePath & "qc_jcl4.txt" & """")
                swFTP.WriteLine("close")
                swFTP.WriteLine("quit")
            End Using

            Shell(psFTPpath & " -s:" & """" & psFilePath & "qc_ftp4.txt" & """")

Open in new window



                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("mfvipa.amgreetings.com");
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // FTP site logon.
                request.Credentials = new NetworkCredential(sLogonID, sPass);

                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(@"C:\Users\dcastling\jclDC11ID3.txt");
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

The URI you pass to WebRequest.Create needs to be the full URI, including the path and filename of what should be created on the server.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kimputer
Kimputer

Just to clarify, VB and C# compile to the same code, and it's mostly new syntax what you're now learning (almost a 1 on 1 translation of what you're used to do in VB). Therefore the easy solution would be from @Carl_Tawn.
But if you really want to learn new stuff, continue with what you were doing and fix this line:

       FtpWebRequest request = (FtpWebRequest)WebRequest.Create("mfvipa.amgreetings.com");

to
       FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mfvipa.amgreetings.com/jclDC11ID3.txt");
Avatar of coperations07

ASKER

Thanks for the quick replies guys!

I had actually started out trying what Carl is suggesting. The way I had it set up, the ftp prompt would open and show msg Unknown host C:\Users\dcastling\DC11ID3ftp.txt.
The file DC11ID3ftp.txt contains the line commands to pass to the ftp prompt.

I changed the URI as suggested. I'm getting an exception on line Stream requestStream = request.GetRequestStream();
The remote server returned an error: (501) Syntax error in parameters or arguments.

So I'm trying to figure out what's going on with that...
Thanks Carl. This got me going.