728x90

C# comboBox 에서 POST 방식으로 서버에 값을 전달하고 서버에서 그 결과를 회신하는 코드에 대해 작성한다.


private void cat1comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    if (cat1comboBox1.SelectedIndex > 0)
    {
        cat1name1 = ((KeyValuePair<string, string>)cat1comboBox1.SelectedItem).Key;
        cat1uid1 = ((KeyValuePair<string, string>)cat1comboBox1.SelectedItem).Value;
        cat2comboBox(cat1uid1, cat2comboBox1); // 두번째 comboBox 표시
    }
}

void cat2comboBox(string category1, ComboBox CB)
{
    Dictionary<string, string> cat2Source = new Dictionary<string, string>();
    cat2Source.Add("전체", "0");
    try
    {
        string URL = "http://IP주소/cat2comboBox.php";
        string json = POSTPHPJSON(URL, category1);
        if (json != null)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            JsonHelper2 category2 = serializer.Deserialize<JsonHelper2>(json);

            foreach (var dr in category2.result)
            {
                cat2Source.Add(dr.classname, dr.uid);
            }

            CB.DataSource = new BindingSource(cat2Source, null);
            CB.DisplayMember = "Key";
            CB.ValueMember = "Value";

            CB.SelectedIndex = 0;  // 첫번째 아이템 선택
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

private void cat2comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    if (cat2comboBox1.SelectedIndex > 0)
    {
        cat2name1 = ((KeyValuePair<string, string>)cat2comboBox1.SelectedItem).Key;
        cat2uid1 = ((KeyValuePair<string, string>)cat2comboBox1.SelectedItem).Value;
        MessageBox.Show(cat2name1 + " : " + cat2uid1);
    }
}

string POSTPHPJSON(string url, string Data)
{
    try
    {
        var webRequest = (HttpWebRequest)WebRequest.Create(url);
        var postData = string.Format("catagory1={0}", Data); 

        // 좀 더 정교한 함수로 처리하려면 이 부분은 함수 밖에서 처리하는게 좋다.

        webRequest.Method = "POST";
        byte[] sendData = UTF8Encoding.UTF8.GetBytes(postData);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = sendData.Length;

        Stream dataStream = webRequest.GetRequestStream();
        dataStream.Write(sendData, 0, sendData.Length);
        dataStream.Close();

        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
        {
            var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8, true);
            return @reader.ReadToEnd().Trim();
        }
        else
        {
            MessageBox.Show(string.Format("Status code == {0}", webResponse.StatusCode));
            return null;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return null;
    }
}

public class cat2comboBox
{
    public string uid { get; set; }
    public string classname { get; set; }
}

public class JsonHelper2
{
    public List<cat2comboBox> result { get; set; }
}


728x90
블로그 이미지

Link2Me

,