c# 웹 서버에 데이터 게시

        /// <summary>
        /// Post 
        /// </summary>
        /// <param name="posturl"> </param>
        /// <param name="postData"> </param>
        /// <returns></returns>
        public string GetPage(string posturl, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
            byte[] data = encoding.GetBytes(postData);
            //  ...
            try
            {
                //  
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                // 
                response = request.GetResponse() as HttpWebResponse;
                // request.GetResponse() Post 
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                // (html) 
                string content = sr.ReadToEnd();
                string err = string.Empty;
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }

좋은 웹페이지 즐겨찾기