Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use 'using' instead of closing a WebResponse and StreamReader

Currently

I've implemented a simple helper method for HttpWebRequest called GetResponse(url). Currently I'm manually closing the WebResponse and StreamReader after reading the result. I'm then returning said result like so:

// construct the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

// get the result
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();

// clean up and return the result
reader.Close();
response.Close();
return result;

Proposed

Is it safe to encompass the return within using statements instead of closing them; will this have the same effect as the .Close()es?

// construct the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

// get the result
using (WebResponse response = request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        return reader.ReadToEnd();
    }
}
like image 854
Richard Avatar asked Oct 12 '12 14:10

Richard


People also ask

How WebRequest works?

The browser sends an HTTP request message to the server, asking it to send a copy of the website to the client (you go to the shop and order your goods). This message, and all other data sent between the client and the server, is sent across your internet connection using TCP/IP.

What is response Stream?

A Stream containing the body of the response.

What is System net WebRequest?

This is a simple application that the gets the source of a webpage via the WebRequest Object. The WebRequest class is defined in System.Net namespace. WebRequest is an abstract class. It is the base class for accessing data from the internet in the . Net Framework.


2 Answers

That's not only safe - it's safer than the original, in that it will dispose of the objects even if an exception is thrown; a using statement is equivalent to a try/finally statement.

In general, whenever you write a Close() or Dispose() call explicitly, consider whether you could use a using statement instead.

(Note that you're not using the encoding from the web response, by the way - you're always assuming UTF-8. Using WebClient instead can make this simpler, if that's an option.)

like image 180
Jon Skeet Avatar answered Nov 03 '22 09:11

Jon Skeet


I would suggest do this:

    string ret = string.Empty;
    using (WebResponse response = request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            ret = reader.ReadToEnd();
        }
    }
    return ret;

it is safe to use "using", it will dispose the WebResponse and StreamReader, but it does not guarantee it will run the return.

like image 22
urlreader Avatar answered Nov 03 '22 09:11

urlreader