1. 程式人生 > 其它 >WebApi HttpWebRequest GET POST 傳值

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