C# HttpWebRequest 方式提交数据,参数为普通键值对

private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)

        {

            string ret = string.Empty;

            try

            {

                byte[] byteArray = dataEncode.GetBytes(paramData); //转化

                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));

                webReq.Method = "POST";

                webReq.ContentType = "application/x-www-form-urlencoded";



                webReq.ContentLength = byteArray.Length;

                Stream newStream = webReq.GetRequestStream();

                newStream.Write(byteArray, 0, byteArray.Length);//写入参数

                newStream.Close();

                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();

                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);

                ret = sr.ReadToEnd();

                sr.Close();

                response.Close();

                newStream.Close();

            }

            catch (Exception ex)

            {

                Response.Write(ex.Message);

            }

            return ret;

        }

  

paramData格式为:a=1&b=2等;

-------------------------
下面为服务器端接收方法:
加载事件中:

//对应PostWebRequest方法

            //Response.Write(string.Format("<?xml version=\"1.0\"?><params><title>{0}</title></params>",Request["a"]));

  

你可能感兴趣的:(request)