WebApi HttpWebRequest GET POST 传值

action

public class EasyModelByRequestUrlController : ApiController
    {
        // GET: api/EasyModelByRequestUrl
        [HttpGet]
        public string Action1(string name,string sex,int age)
        {
            return $"GET请求 姓名{name},性别{sex},年龄{age}";
        }

       
        [HttpPost]
        // POST: api/EasyModelByRequestUrl
        public string Post([FromBody]string name, int age)
        {
            return $"Post请求 姓名{name},年龄{age}";
        }
    }

请求

 

 

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

            #region POST action那里,frombody只能取值一次,第二次就会报错

            //Byte[] bytes = Encoding.UTF8.GetBytes("=123");
            //// 注意,post一个string参数的时候,采取 =XXX,type application/x-www-form-urlencoded;charset=utf-8,才能取到值
            ////注意 action那里,frombody只能取值一次,第二次就会报错
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9892/api/EasyModelByRequestUrl?age=18"); 
            // 传递多个参数,只有一个参数是POST传递,其他通过URL传递,同时action 必须指定 【frombody】
            //request.Method = "POST";

            //request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            //request.ContentLength = bytes.Length;

            #endregion

            #region 采用JSON传值的时候,contentType 采用 application/json;charset=utf-8  ,传递的值需要进行 JSON转换  JsonConvert.SerializeObject(str);


            string jsonstr = JsonConvert.SerializeObject(str);

            Byte[] bytes = Encoding.UTF8.GetBytes(jsonstr);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9892/api/EasyModelByRequestUrl?age=18");
            request.Method = "POST";

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

            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;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            HttpWebRequest request=(HttpWebRequest) WebRequest.Create("http://localhost:9892/api/EasyModelByRequestUrl?name=张三&sex=男&age=16");
            request.Method = "GET";

            //通过URL传值的话,可以不指定 contentType
            //request.ContentType = "text/html;charset=utf-8"; 
            //request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                this.textBox1.Text = result;
            }

        }

  

结果

POST

 

 

GET

 

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