Solved

FtpWebRequest

Posted on 2014-12-16
5
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 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