0

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

4 回答 4

4

这段代码可以被拉入一个带有一些参数的方法中,用于改变参数:

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);
}
于 2012-04-11T12:43:29.650 回答
2

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

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);
    }
}
于 2012-04-11T12:43:57.980 回答
2

是的,您可以创建一个静态类,可以在一个单独的文件中,并在您需要调用 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);
    }
}
于 2012-04-11T12:44:17.333 回答
1

如何创建一个接受一个参数的方法 - 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();
}
于 2012-04-11T12:44:03.963 回答