# C# Class of client methods for interacting with web servers

I'm in QA Automation, and C# isn't my first language. I've written a small class of methods for GETting and POSTing to web URLs. I use these when I want to test certain things that really fall outside the jurisdiction of Selenium, but are still roughly "black box" type testing. Link scanning, and culling data from POSTBIN, are a few examples.

I was wondering if you C# gurus might like to look the code over, and let me know if there are better ways to do what I've done, or if there's any potential gotcha's I haven't thought of:

public class RawHttp
{
public static string Url = null;

public RawHttp(string url)
{
Url = url;
}

public HttpStatusCode GetStatusCode(string url = null)
{
HttpStatusCode result = default(HttpStatusCode);
string activeUrl = url ?? Url;

var request = WebRequest.Create(activeUrl);
using (var response = request.GetResponse() as HttpWebResponse)
{
if (response != null)
{
result = response.StatusCode;
response.Close();
}
}
return result;
}

public string GetBody(string url = null)
{
string activeUrl = url ?? Url;

var request = (HttpWebRequest)WebRequest.Create(activeUrl);
request.Method = "GET";
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
var response = (HttpWebResponse)request.GetResponse();

//Console.WriteLine("Content length is {0}", response.ContentLength);
//Console.WriteLine("Content type is {0}", response.ContentType);

// Get the stream associated with the response.
// Pipes the stream to a higher level stream reader with the required encoding format.
response.Close();
return body;
}

public string Post(string url = null, string parameters = "")
{
string activeUrl = url ?? Url;

var req = WebRequest.Create(activeUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
req.ContentLength = bytes.Length;
Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
var resp = req.GetResponse();
//if (resp == null) {return null;}
}
}


Thanks!

• Hello Greg. Is this strictly used as a utility? Do you test repeated things, like test a=status code success and then b = check something in the body? Also, you are switching between the static "Url" variable and the actual instance (the class is instance based and not static), what is the use case you are shooting for? Is there any other forms of functionality that you want to add onto this in the future? Is this code being included in something else (i.e. how does it interact with your test suite)? – Randy Apr 5 '12 at 0:01
• Thanks for the questions, Randy. I suppose I should have supplied some examples when I created this question. I've not had a lot of time to revisit this, though. I'm sorry for that. I'll respond in more detail later this week. – Greg Gauthier Apr 11 '12 at 21:53

1. You're not calling Dispose() on everything that you strictly should (eg, Streams).
• Using using is even better than calling .Dispose(). – Leonid Apr 5 '12 at 19:56
• From a disposal standpoint they're equivalent, though using is frequently more convenient. At any rate, he's not doing either of them in several cases. – breischl Apr 5 '12 at 20:07