C#发送和接受POST请求

1、发送Post请求代码

     /// <summary>
        /// 发起Http请求
        /// </summary>
        /// <param name="flightData">发送航班对象</param>
        public static void HttpRequest(string url, string para)
        {
            byte[] bs = Encoding.UTF8.GetBytes(para);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }

            string strResponse = string.Empty;
            using (WebResponse wr = req.GetResponse())
            {
                StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                strResponse = responseStream.ReadLine();
            }
        }

2、接受Post请求

       if (!string.IsNullOrEmpty(this.Request.Form.ToString()))
            {
                string str = HttpUtility.UrlDecode(this.Request.Form.ToString(), Encoding.UTF8);
            }

 

posted on 2014-09-18 17:30  BestNow  阅读(923)  评论(0编辑  收藏  举报

导航