Link to home
Start Free TrialLog in
Avatar of racy
racy

asked on

c# webresponse status

Hello.  New to the Windows world and .NET programming.  Below is a snippet of code (pulled from the internet) that get's a listing of files from an FTP server.  The try/catch code check for the success/failure of the whole operation, but I'd like to get a status from the "WebResponse response = reqFTP.GetResponse();" line of code.  How can I get the status?

Thank you
FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse response = reqFTP.GetResponse();
                
                StreamReader reader = new StreamReader(response.GetResponseStream());
 
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                // to remove the trailing '\n'
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                downloadFiles = null;
                Console.WriteLine("Error {0}encountered while getting file list", ex);
                Console.ReadLine();
                return downloadFiles;
                
                
            }

Open in new window

SOLUTION
Avatar of Gary Davis
Gary Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of racy
racy

ASKER

So are you saying to code like:

string myStatus = response.Status;

??  This is where I am getting hung up.  The response variable doesn't seem to have a "Status" method.
Avatar of racy

ASKER

According to MSDN, "GetResponse" sets the Status property of WebExceptionStatus.  This is what I am trying to get.
Avatar of racy

ASKER

I figured this one out.  Must something like:

catch (WebException e) {
.
.
.
}

I am such a newbie.  
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial