Solved

FtpWebRequest

Posted on 2014-12-16
5
160 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with preventing selection from a combobox 11 35
Changes to XXX.exe.config file are not retrieved on restart. 3 40
Where is this file? 3 26
VS 2017 18 57
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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