G Gr
G Gr

Reputation: 6090

Code being reused over and over again, a more manageable way?

So I noticed im starting to re-use code over and over again and its starting to look ugly. Each button click I noticed that some of the code (For POST) is the same apart from the uri. Is there any way I could manage the below code in a better way?

    private void AddTag_Click(object sender, EventArgs e)
    {
        string uriAddTagtoGroup = string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}", textBox6.Text, textBox7.Text);
        byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
        req.Method = "POST";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        MessageBox.Show(resp.StatusDescription);
        reqStrm.Close();
        resp.Close();
    }

Upvotes: 0

Views: 140

Answers (4)

Shakti Prakash Singh
Shakti Prakash Singh

Reputation: 2533

Yes, you can create a static class, may be in a separate file and call it when you need to call the routine for post:

private void AddTag_Click(object sender, EventArgs e)
{
    string uriAddTagtoGroup = 
        string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}",
            textBox6.Text, textBox7.Text);
    PostRoutine(uriAddTagtoGroup);        
}

public static void PostRoutine(string uri)
{
    try
    {
        byte[] arr = Encoding.UTF8.GetBytes(uri);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        MessageBox.Show(resp.StatusDescription);
        reqStrm.Close();
        resp.Close();
    }
    catch(Exception ex)
    {    
        MessageBox.Show(ex.Message);
    }
}

Upvotes: 2

Gero
Gero

Reputation: 13563

How about creating a method which accepts one parameter - the URI

private void AddTag_Click(object sender, EventArgs e)
{
   string uriAddTagtoGroup = 
      string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}",
         textBox6.Text, textBox7.Text);
   someMethod(uriAddTagtoGroup);
} 

private void someMethod(String uriAddTagtoGroup)
{
    byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
    req.Method = "POST";
    req.ContentType = "application/xml";
    req.ContentLength = arr.Length;
    Stream reqStrm = req.GetRequestStream();
    reqStrm.Write(arr, 0, arr.Length);
    reqStrm.Close();
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    MessageBox.Show(resp.StatusDescription);
    reqStrm.Close();
    resp.Close();
}

Upvotes: 1

SwDevMan81
SwDevMan81

Reputation: 50018

As others pointed out in the comments, just encapsulate it into a method:

private void AddTag_Click(object sender, EventArgs e)
{
    string uriAddTagtoGroup = 
       string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}",
          textBox6.Text, textBox7.Text);
    RequestResponse(uriAddTagtoGroup)
}

private void RequestResponse(string uriAddTagtoGroup)
{
    byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
    req.Method = "POST";
    req.ContentType = "application/xml";
    req.ContentLength = arr.Length;
    using(Stream reqStrm = req.GetRequestStream())
    {
       reqStrm.Write(arr, 0, arr.Length);
    }
    using(HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
    {
       MessageBox.Show(resp.StatusDescription);
    }
}

Upvotes: 2

Adam Houldsworth
Adam Houldsworth

Reputation: 64517

This code can be pulled into a method with some parameters for the arguments that change:

private void AddTag_Click(object sender, EventArgs e)
{
    string uriAddTagtoGroup = string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}", textBox6.Text, textBox7.Text);
    PerformPost(uriAddTagtoGroup);
}

public void PerformPost(string uri)
{
    byte[] arr = Encoding.UTF8.GetBytes(uri);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.Method = "POST";
    req.ContentType = "application/xml";
    req.ContentLength = arr.Length;
    Stream reqStrm = req.GetRequestStream();
    reqStrm.Write(arr, 0, arr.Length);
    reqStrm.Close();
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    MessageBox.Show(resp.StatusDescription);
    reqStrm.Close();
    resp.Close();
}

For disposable stuff (things that implement IDisposable), there is also the using keyword:

using (var resp = req.GetResponse())
{
    MessageBox.Show(resp.StatusDescription);
}

Upvotes: 4

Related Questions