代码一遍又一遍地重用,一种更易于管理的方式

本文关键字:一遍 易于 管理 一种 方式 代码 | 更新日期: 2023-09-27 18:30:40

所以我注意到我开始一遍又一遍地重用代码,它开始看起来很丑陋。每次单击按钮,我都注意到除了 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();
    }

代码一遍又一遍地重用,一种更易于管理的方式

可以将此代码拉入一个方法中,其中包含一些用于更改的参数的参数:

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

对于一次性物品(实现IDisposable的东西),还有using关键字:

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

正如其他人在评论中指出的那样,只需将其封装到一个方法中:

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

是的,你可以创建一个静态类,可以在一个单独的文件中,当你需要调用 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);
    }
}

如何创建一个接受一个参数的方法 - 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();
}