Solved

FtpWebRequest

Posted on 2014-12-16
5
139 Views
Last Modified: 2014-12-16
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

0
Comment
Question by:coperations07
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40502853
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.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40502881
You don't really need to use the FtpWebRequest.  If you're trying to replace the VB like-for-like, then you can replace the call to Shell() with something like:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo
{
     FileName = psFTPpath,
     Arguments = string.Format("-s:\"{0}qc_ftp4.txt\"", psFilePath)
};
p.Start();

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40502896
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");
0
 

Author Comment

by:coperations07
ID: 40503063
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...
0
 

Author Closing Comment

by:coperations07
ID: 40503496
Thanks Carl. This got me going.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now