?
Solved

FtpWebRequest Double Login?

Posted on 2012-12-20
4
Medium Priority
?
378 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
[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
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38711916
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
ID: 38712554
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
ID: 38712556
Also trying to eliminate redundant code.
0
 

Author Closing Comment

by:pointeman
ID: 38725622
Good point, thank
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
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
Course of the Month15 days, left to enroll

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