2012-04-11 6 views
0

Also habe ich bemerkt, dass ich Code immer und immer wieder benutze und es anfängt, hässlich auszusehen. Mit jedem Knopfdruck bemerkte ich, dass ein Teil des Codes (für POST) abgesehen von der URI gleich ist. Gibt es eine Möglichkeit, den unten stehenden Code besser zu verwalten?Code wieder und wieder verwendet werden, ein überschaubarer Weg?

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(); 
    } 
+6

Sieht aus wie Code, der in einer Methode trivial eingekapselt werden kann. Wo ist dein Problem? Etwas wie 'string ExecuteRequest (string uri)' – CodesInChaos

+2

Der URI wäre ein Parameter für diese Methode. Nochmal: Wo ist dein Problem? –

+0

Ich denke, das Problem wird bald sarky Antworten auf eine Frage sein lol –

Antwort

4

Dieser Code kann für die Argumente mit einigen Parametern in ein Verfahren gezogen werden, die sich ändern:

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(); 
} 

Für Einweg-Sachen (Dinge, die IDisposable implementieren), gibt es auch die using Stichwort:

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

Ahhh ... Danke, Adam, es ist wirklich einfach! –

2

Wie andere in den Kommentaren darauf hingewiesen, kapseln nur in eine Methode:

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); 
    } 
} 
1

Wie wäre es, ein Verfahren zu schaffen, die einen Parameter akzeptiert - die 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(); 
} 
2

Ja, können Sie eine statische Klasse erstellen, in einer separaten Datei sein kann und es nennen, wenn Sie für die Routine aufrufen müssen 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); 
    } 
} 
Verwandte Themen