WEBAPI 传递实体对象,数组,集合,字典

 

 

传递实体对象,首先定义负责对象

public class  Student
    {
        public string name { get; set; }
        public string sex { get; set; }
        public string age { get; set; }
        public Adress adress { get; set; }
    }
    public class Adress
    {
        public string country { get; set; }
        public string province { get; set; }
        public string city { get; set; }
        public string county { get; set; }

        public override string ToString()
        {
            return $"地址是{country}{province}省{city}市{county}县"; ;
        }

请求

private void button1_Click(object sender, EventArgs e)
        {
             string str = "张三";

            var adress = new { country="中国", province="江苏",city="XXX安",county="XXX" };

            var student = new {name="张老三",sex="雄性",age=29,adress= adress };


            Byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(student));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9892/api/EasyModelByRequestUrl");
            request.Method = "POST";

            request.ContentType = "application/json;charset=utf-8";
            request.ContentLength = bytes.Length;




            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader=new StreamReader( response.GetResponseStream()))
            {
                string result=reader.ReadToEnd();
                this.textBox1.Text = result;
            }

        }

接收

[HttpPost]
        // POST: api/EasyModelByRequestUrl
        public string Post(Student student)
        {


            return $"Post请求 姓名{student.name},性别{student.sex},年龄{student.age},{student.adress}";
        }

传递数组

 

 请求

private void button1_Click(object sender, EventArgs e)
        {
             string[] str = {"第一页", "第二页", "第三页", "第四页" };



            Byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(str));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9892/ArrayMethod");
            request.Method = "POST";

            request.ContentType = "application/json;charset=utf-8";
            request.ContentLength = bytes.Length;




            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader=new StreamReader( response.GetResponseStream()))
            {
                string result=reader.ReadToEnd();
                this.textBox1.Text = result;
            }

        }

接收

[HttpPost]
        [Route("ArrayMethod")]
        // POST: api/EasyModelByRequestUrl
        public string Post2(string[] array)
        {
            string str = "";
            if (array.Length>0)
            {
                str = string.Join("|", array);
            }

            return str;
        }

  

传递集合

 

 

请求

private void button1_Click(object sender, EventArgs e)
        {
            var adress = new { country = "中国", province = "江苏", city = "XXX安", county = "XXX" };

            var student1 = new { name = "张老三", sex = "雄性", age = 29, adress = adress };
            var student2 = new { name = "李老四", sex = "雄性", age = 39, adress = adress };
            var student3 = new { name = "王老五", sex = "雄性", age = 49, adress = adress };
            List<object> list = new List<object>();
            list.Add(student1);
            list.Add(student2);
            list.Add(student3);

            Byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(list));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9892/ListMethod");
            request.Method = "POST";

            request.ContentType = "application/json;charset=utf-8";
            request.ContentLength = bytes.Length;




            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader=new StreamReader( response.GetResponseStream()))
            {
                string result=reader.ReadToEnd();
                this.textBox1.Text = result;
            }

        }

接收

[HttpPost]
        [Route("ListMethod")]
        // POST: api/EasyModelByRequestUrl
        public string Post3(List<Student> list)
        {
            string str = "";
            if (list.Count > 0)
            {
                list.ForEach(item=> {
                    str += item;
                });
            }

            return str;
        }

  

传递字典

  

 

 请求

private void button1_Click(object sender, EventArgs e)
        {
            var adress = new { country = "中国", province = "江苏", city = "XXX安", county = "XXX" };

            var student1 = new { name = "张老三", sex = "雄性", age = 29, adress = adress };
            var student2 = new { name = "李老四", sex = "雄性", age = 39, adress = adress };
            var student3 = new { name = "王老五", sex = "雄性", age = 49, adress = adress };
            Dictionary<string, Object> dic = new Dictionary<string, Object>();
            dic.Add("student1", student1);
            dic.Add("student2", student2);
            dic.Add("student3", student3);

            Byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dic));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9892/ListMethod");
            request.Method = "POST";

            request.ContentType = "application/json;charset=utf-8";
            request.ContentLength = bytes.Length;




            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader=new StreamReader( response.GetResponseStream()))
            {
                string result=reader.ReadToEnd();
                this.textBox1.Text = result;
            }

        }

接收

[HttpPost]
        [Route("ListMethod")]
        // POST: api/EasyModelByRequestUrl
        public string Post3(IDictionary<string,Student> dic)
        {
            string str = "";
            if (dic.Count > 0)
            {
                foreach(KeyValuePair<string,Student> pair in dic)
                {
                    str += pair.Value;
                }
            }

            return str;
        }

  

  

  

 

posted @ 2021-11-01 17:23  zq爱生活爱代码  阅读(445)  评论(0编辑  收藏  举报