HttpWebRequest post 用法

 

本文章示範 HttpWebRequest post 參數有兩種方法,分別如下

 

一、application/json

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://54.251.57.223:9999/api/ListTai";
            string theMessage = "";

            //request
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("Authorization", "token");
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = JsonConvert.SerializeObject(new
                {
                    agencyId = "agency1",
                    itemList = new[] {
                                new {
                                    barCode ="number1",
                                    attrCategory = "category1",
                                    attrName =  "name1"
                                },
                                new {
                                    barCode ="number2",
                                    attrCategory = "category2",
                                    attrName =  "name2"
                                }
                            }
                });

                streamWriter.Write(json);
            }

            //response
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                string result = streamReader.ReadToEnd();

                JObject jResult = JObject.Parse(result);

                theMessage = (string)jResult["message"];
            }

        }
    }
}

 

二、application/x-www-form-urlencoded

using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://54.251.57.223:9999/test";
            string theMessage = "";

            //request
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("Authorization", "token");

            //要傳送的資料內容(依字串表示)
            string postData = "id=9&name=Tom";
            //將傳送的字串轉為 byte array
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            //告訴 server content 的長度
            httpWebRequest.ContentLength = byteArray.Length;

            using (Stream reqStream = httpWebRequest.GetRequestStream())
            {
                reqStream.Write(byteArray, 0, byteArray.Length);
            }

            //response
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                theMessage = streamReader.ReadToEnd();
            }

        }
    }
}

 

參考資料:

[C#/.net] 使用HttpWebRequest來Post資料

如何使用 WebRequest,HttpWebRequest 來存取(GET,POST,PUT,DELETE,PATCH)網路資源

Request 和 Response 的.NET技術應用