?
Solved

FtpWebRequest

Posted on 2014-12-16
5
Medium Priority
?
199 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 2000 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 36

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 Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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