C#Http 서버에 POST 요청 보내기

2482 단어
       // Http  POST    
        public string m_PostSubmit(string strUrl,string strParam)
        {
            string strResult = "error";
            try
            {
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strUrl);
                Encoding encoding = Encoding.UTF8;
                //encoding.GetBytes(postData);
                byte[] bs = Encoding.ASCII.GetBytes(strParam);
                string responseData = System.String.Empty;
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = bs.Length;
                try
                {
                    using (System.IO.Stream reqStream = req.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                        reqStream.Close();
                    }
                    using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)req.GetResponse())
                    {
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                        {
                            responseData = reader.ReadToEnd().ToString();
                            strResult = responseData;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    strResult = "error:" + ex.Message;// 
                }
            }
            catch (System.Exception ex)
            {
                strResult = "error:" + ex.Message;// 
            }
            return strResult;
        }


string strURL = "http://localhost/WinformSubmit.php";
//string param = string.Format("do=login&u={0}&p={1}", username, password);//매개 변수의 또 다른 쓰기
string strCallerId="1398888888";
string strParam = "callerid="+ strCallerId ; string strResult = this.m_PostSubmit(strUrl,strParam);
http 섹션
WinformSubmit.php 페이지
if($_POST['callerid']=='139') {echo("");}else {echo ("아니오");}

좋은 웹페이지 즐겨찾기