Solved

FtpWebRequest Double Login?

Posted on 2012-12-20
4
369 Views
Last Modified: 2012-12-27
I would like to consolidate my code so the 'Login' method is called and the FtpWebRequest would no longer be needed in the 'Upload' method. As is, the FTP server logs show 2 logins. Help!

 
public static FtpWebRequest request = null;

public static bool Login()
        {
            try
            {
                request = (FtpWebRequest) WebRequest.Create(new Uri(_uri));
                request.Credentials = new NetworkCredential(_userName, _password);
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.UseBinary = _useBinary;
                request.EnableSsl = _enableSsl;
                request.KeepAlive = _keepAlive;
                request.UsePassive = _usePassive;
                request.Timeout = _timeOut;

                using (var response = (FtpWebResponse) request.GetResponse())
                {
                    statusMessage(response.StatusDescription);
                }
                
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static void Upload()
        {
            if (Login())
            {
                try
                {
                    request = (FtpWebRequest) WebRequest.Create(_uri + _fileName);
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request.Credentials = new NetworkCredential(_userName, _password);

                    using (var sourceStream = new StreamReader(_filePath + _fileName))
                    {
                        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
                        request.ContentLength = fileContents.Length;

                        using (Stream requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(fileContents, 0, fileContents.Length);
                        }
                    }

                    using (var response = (FtpWebResponse) request.GetResponse())
                    {
                        statusMessage(response.StatusDescription);
                    }
                }
                catch (Exception ex)
                {
                    statusMessage(ex.Message);
                }
            }
        }

Open in new window

0
Comment
Question by:pointeman
  • 3
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Why have the Login method at all? You have to send credentials any time you connect to an FTP server. What are you looking to gain?
0
 

Author Comment

by:pointeman
Comment Utility
I want a separate 'response' for login. So if login returns false, I now it was a login failure and not a Upload or Download error.
0
 

Author Comment

by:pointeman
Comment Utility
Also trying to eliminate redundant code.
0
 

Author Closing Comment

by:pointeman
Comment Utility
Good point, thank
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

11 Experts available now in Live!

Get 1:1 Help Now