如何使用C#使用httpwebrequest(form-data)发布JSON格式的数据?

时间:2019-09-12 13:25:21

标签: c# api httpwebrequest

我正在尝试使用C#中的HttpWebrequest将带有“表单数据”中的JSON数据的请求发布到服务器。

我尽了最大努力,但未获得任何成功的结果。我什至尝试使用WebClient,但未成功。 请找到下面我尝试过的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.IO;
using Newtonsoft.Json;
using System.Web;


namespace Console_Workstation
{
    class UpdatingSrialnumbersvia_API
    {
        #region Properties
        public class JsonContent
        {
            public DateTime date { get; set; }
            public string reason { get; set; }
            public string description { get; set; }
            public string reference_number { get; set; }
            public string adjustment_type { get; set; }
            public List<line_items> line_item { get; set; }
        }

        public class line_items
        {
            public long item_id { get; set; }
            public string name { get; set; }
            public int quantity_adjusted { get; set; }
            public List<string> serial_numbers { get; set; }
            public int item_total { get; set; }
            public string unit { get; set; }
            public bool is_combo_product { get; set; }
            public string adjustment_account_name { get; set; }
            public long warehouse_id { get; set; }
            public string warehouse_name { get; set; }
        }
        #endregion

        public static void Main()
        {
            #region JSon Content

            line_items items = new line_items
            {
                item_id = 519558000000686015,
                name = "Acer 19.5-inch LED Monitor (K202HQL-AB)",
                quantity_adjusted = 1,
                serial_numbers = new List<string>()
            {
                "TPS5678AMZ"
            },
                item_total = 15,
                unit = "qty",
                is_combo_product = false,
                adjustment_account_name = "Cost of Goods Sold",
                warehouse_id = 519558000000076101,
                warehouse_name = "QSAI"
            };
            JsonContent content = new JsonContent
            {
                date = new DateTime(2019 - 09 - 12),
                reason = "Extra Stock",
                description = "",
                reference_number = "Test-IA-456",
                adjustment_type = "quantity",
                line_item = new List<line_items>() { items }
            };

            #endregion
            try
            {
                string json = JsonConvert.SerializeObject(content, Formatting.Indented);

                string strResponse = string.Empty;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://inventory.zoho.com/api/v1/inventoryadjustments?organization_id=123456789");
                request.Method = "POST";
                request.Headers.Add("Authorization", "*******************************");
                request.ContentType = "multipart/form-data";
                request.Host = "inventory.zoho.com";
                request.Headers.Add("name", "JsonString=" + json);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new ApplicationException("Error code in response recieved: " + response.StatusCode.ToString());
                    }
                    using (Stream stream = response.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (StreamReader streamReader = new StreamReader(stream))
                            {
                                strResponse = streamReader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadKey();
            }
        }
    }
}

JSON格式如下

{
    "date": "2019-09-11",
    "reason": "Extra Stock",
    "description": "",
    "reference_number": "Test-IA-123",
    "adjustment_type": "quantity",
    "line_items": [
        {
            "item_id": 519558000000686015,
            "name": "Acer 19.5-inch LED Monitor (K202HQL-AB)",
            "quantity_adjusted": 1,
            "serial_numbers":[
              "TPS1234AMZ" 
              ],
            "item_total": 15,
            "unit": "qty",
            "is_combo_product": false,
            "adjustment_account_name": "Cost of Goods Sold",
            "warehouse_id": 519558000000076101,
            "warehouse_name": "QSAI"
        }
    ]
}

PostMan图片的PFA

enter image description here

0 个答案:

没有答案