Could someone please tell me if the code below is correct as far as "using","close" and "try-catch" is concerned? Not at all a C#/CLR guy, inherited a clunky piece of code and trying to get it sorted:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Globalization;
// For the SQL Server integration
using Microsoft.SqlServer.Server;
// Other things we need for WebRequest
using System.Net;
using System.Text;
using System.IO;
public partial class UserDefinedFunctions
{
// Function to return a web URL as a string value.
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString GET(SqlString uri, SqlString username, SqlString passwd)
{
// The SqlPipe is how we send data back to the caller
SqlPipe pipe = SqlContext.Pipe;
SqlString document;
try
{
// Set up the request, including authentication
WebRequest req = WebRequest.Create(Convert.ToString(uri));
if (Convert.ToString(username) != null & Convert.ToString(username) != "")
{
req.Credentials = new NetworkCredential(
Convert.ToString(username),
Convert.ToString(passwd));
}
((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";
// Fire off the request and retrieve the response.
using (WebResponse resp = req.GetResponse())
{
using (Stream dataStream = resp.GetResponseStream())
{
//SqlContext.Pipe.Send("...get the data");
using (StreamReader rdr = new StreamReader(dataStream))
{
document = (SqlString)rdr.ReadToEnd();
rdr.Close();
}
// Close up everything...
dataStream.Close();
}
resp.Close();
// .. and return the output to the caller.
return (document);
}//end using
}
catch (WebException e)
{
document = e.ToString();
return (document);
throw;
}
}
// Function to submit a HTTP POST and return the resulting output.
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString POST(SqlString uri, SqlString postData, SqlString username, SqlString passwd)
{
SqlPipe pipe = SqlContext.Pipe;
SqlString document;
byte[] postByteArray = Encoding.UTF8.GetBytes(Convert.ToString(postData));
// Set up the request, including authentication,
// method=POST and encoding:
try
{
WebRequest req = WebRequest.Create(Convert.ToString(uri));
((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";
if (Convert.ToString(username) != null & Convert.ToString(username) != "")
{
req.Credentials = new NetworkCredential(
Convert.ToString(username),
Convert.ToString(passwd));
}
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
// Submit the POST data
using (Stream dataStream = req.GetRequestStream())
{
dataStream.Write(postByteArray, 0, postByteArray.Length);
dataStream.Close();
}
// Collect the response, put it in the string variable "document"
using (WebResponse resp = req.GetResponse())
{
Stream dataStream = resp.GetResponseStream();
using (StreamReader rdr = new StreamReader(dataStream))
{
document = (SqlString)rdr.ReadToEnd();
rdr.Close();
}
dataStream.Close();
resp.Close();
}
return (document);
}//end try
catch (WebException e)
{
document = e.ToString();
return (document);
throw;
}//end catch
}
}
I have been searching and checking every StackOverflow post but I just cannot seem to get this sorted in my head. Do I need "close" in each of the "using" blocks? Or do the "using" blocks call "Dispose" automatically and therefore make the "close" irrelevant?