Solved

FtpWebRequest Double Login?

Posted on 2012-12-20
4
377 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

690 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