Solved

c# webresponse status

Posted on 2009-07-02
5
647 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:racy
[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
  • 2
5 Comments
 
LVL 18

Assisted Solution

by:Gary Davis
Gary Davis earned 125 total points
ID: 24765191
You should be able to get the status from the response variable assuming it did not trap. A trap will occur in some situations like a bad Uri or a timeout, perhaps.
In addition, you could have additional "catch"es which will catch specific exceptions rather than the general "Exception" for additional reasons for the trap.
Gary Davis
0
 

Author Comment

by:racy
ID: 24765341
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.
0
 

Author Comment

by:racy
ID: 24765408
According to MSDN, "GetResponse" sets the Status property of WebExceptionStatus.  This is what I am trying to get.
0
 

Author Comment

by:racy
ID: 24765555
I figured this one out.  Must something like:

catch (WebException e) {
.
.
.
}

I am such a newbie.  
0
 
LVL 18

Accepted Solution

by:
Gary Davis earned 125 total points
ID: 24766227
You can have these three all at once:
catch (HttpException e) {
.
}
catch (WebException e) {
.
}
catch (Exception e) {
.
}
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but breaks when I add one section 4 35
c#, datatable, aspx 4 53
Example code 13 45
XML Constructor Throwing Incorrectly Structured Document Error 4 25
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 article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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