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

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 に答える