Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#将JSON格式的数据与httpwebrequest(表单数据)一起发布?_C#_Api_Httpwebrequest - Fatal编程技术网

如何使用c#将JSON格式的数据与httpwebrequest(表单数据)一起发布?

如何使用c#将JSON格式的数据与httpwebrequest(表单数据)一起发布?,c#,api,httpwebrequest,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

我正在尝试使用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();
            }
        }
    }
}
邮递员图像的PFA


您是否尝试运行Fiddler来比较C#和Postman的请求?结果如何?您是否收到来自远程服务器的消息?或者您自己的代码是否抛出错误?这是我得到的错误:System.ArgumentException:指定的值具有无效的HTTP头字符。参数名称:System.Net.WebHeaderCollection中的名称。System.Net.WebHeaderCollection中的CheckBadChars(字符串名称,布尔值)。Console_工作站上的Add(字符串名称,字符串值)。UpdatengsRialNumbersvia_API.Main()在C:\Users\AbdulHameedM\source\repos\Console\u Workstation\Console\u Workstation\updateingsrialnumbersvia\u API.cs中:第89行
{
    "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"
        }
    ]
}