Solved

FtpWebRequest Double Login?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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