Option C instead:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
}
But yes, I would close the request stream. Perhaps this is not entirely necessary, but I would not want to assume that - and in particular, if you have no reason to believe that this is not required (for example, documentation), the implementation may change over time and break the "lazy" code later.
If you do this often, you can always write a useful method (perhaps even as an extension method):
public static void WriteRequestData(this WebRequest request, byte[] data)
{
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
}
}
Then just name it like:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.WriteRequestData(data);
source
share