所以我注意到我开始一遍又一遍地重用代码,而且它开始看起来很丑。每次单击按钮我都注意到除了 uri 之外,一些代码(对于 POST)是相同的。有什么办法可以更好地管理下面的代码吗?
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();
}