C# HttpWebRequest post提交数据

//属于客户端
//要向URL Post的方法
public void PostResponse()
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://demo2.cm-force.com/appapi/apiaccount/aa");//这是你要提交的url
            Encoding encoding = Encoding.UTF8;
            
            //参数少的情况下,直接拼接参数字符串 
//string param = "UserName=123&UserPwd=123";//post 参数
 
           /*
           //这是要提交的model对象
            Car c = new Car();
            c.Passed = true;//true
            c.LinkTel = "小测试";
            c.CarBrand = "11111111";
            c.Loads = 101;
            c.UserId = 50;
            c.SortId = 1;
            c.CarArea = "11111111";
            c.CarStateId = 1;
            c.LinkMan = "1111111111";
            c.Sfzh = "141124188789786031";
            c.CarNum = "ABCDE1";
            c.CarLength = 100;
            c.DrivingNum = "11111111";
            c.State = 1;
            c.CarId = 0;
            c.CarOwner = "111111";
            c.CarAreaId = 1;
            c.CarTypeId = 1;
            c.AddTime = DateTime.Now;
            */
           
            /*1、如果对象参数比较多的情况下,使用Dictionary字典提交数据,这样比较清晰。(针对对象)
              2、如果字段比较少的话,就直接通过拼接参数提交。例如:UserName=123&UserPwd=123 (针对少量参数)*/
            
            //比照上面的Car对象转成Dictionary<string, string>泛型集合
            IDictionary<string, string> para = new Dictionary<string, string>();
            para.Add("LinkTel", "第二次测试");
            para.Add("CarBrand", "1111");
            para.Add("Loads", "101");
            para.Add("UserId", "50");
            para.Add("SortId", "1");

            StringBuilder buffer = new StringBuilder();//这是要提交的数据
              int i = 0;

            //通过泛型集合转成要提交的参数和数据
              foreach (string key in para.Keys)
            {
                if (i > 0)
                {
                    buffer.AppendFormat("&{0}={1}", key, para[key]);
                }
                else
                {
                    buffer.AppendFormat("{0}={1}", key, para[key]);
                }
                i++;
            } 

            //通过泛型转化得到的提交数据:LinkTel=第二次测试&CarBrand=1111&Loads=101&UserId=50&SortId=1
            //其实与上面的直接拼接参数无异,
              byte[] bs = Encoding.UTF8.GetBytes(buffer.ToString());//UTF-8

            string responseData = String.Empty;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    responseData = reader.ReadToEnd().ToString();
                }
                Response.Write(responseData);
            }
        }
 

 

posted @ 2021-04-24 12:02  桃花雪  阅读(864)  评论(0编辑  收藏  举报