JSON HttpWebRequest 总是 returns 400

JSON HttpWebRequest always returns 400

我试图提出一个请求来测试人们是否应该能够在发票上付款。

我之前从未用 JSON 数据进行过 API 调用,所以我通读了一些文档和堆栈溢出问题,发现这个线程就在这里,非常有前途:How to post JSON to a server using C#?

我尝试执行此操作的代码是下面发布的代码:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://service-zs.riskcube.ch/api/v1/RiskCube/claim");
        request.ContentType = "application/json";
        request.Method = "POST";
        request.Headers.Add("X-API-KEY", "123"); //Replace with the API KEY given from CreditForm
        if (creditReformModel != null)
        {
            string postData = JsonConvert.SerializeObject(creditReformModel);
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(postData);
            }
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

我尝试实现问题的答案,但遗憾的是,当我尝试发送 httpRequest 时,总是收到 400 错误请求错误。

错误消息显示:System.Net.WebException:“远程服务器响应错误:(400) 错误请求”

这些是异常的详细信息:

System.Net.WebException
  HResult=0x80131509
  Message = The remote server returned an error: (400) Invalid request.
  Source = <The exception source cannot be evaluated.>.
  Stack monitoring:
<The exception stack monitor cannot be evaluated.>.

这就是我要序列化并添加到 httpWebRequest 的模型:

    internal class CreditReformModel
    {
        public string ShopId { get; set; }
        public string OrderProcessId { get; set; }
        public string IpAddress { get; set; }
        public string MacAddress { get; set; }
        public string CustomerId { get; set; }
        public CreditReformAddressModel ShippingAddress { get; set; }
        public CreditReformAddressModel BillingAddress { get; set; }
    }

    internal class CreditReformAddressModel
    {
        public string Type { get; set; }
        public string BusinessName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Co { get; set; }
        public string Street { get; set; }
        public string HouseNumber { get; set; }
        public string PostCode { get; set; }
        public string LocationName { get; set; }
        public string Country { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string DateOfBirth { get; set; }
    }

这是 API 文档中请求的数据:

{
  "shopId": "20071992",
  "orderProcessId": "ref001",
  "ipAddress": null,
  "macAddress": null,
  "customerId": "cus001",
  "billingAddress": {
    "type": "Consumer",
    "businessName": null,
    "firstName": "Martin",
    "lastName": "Früh",
    "co": null,
    "street": "Funkenbüelstrasse",
    "houseNumber": "1",
    "postCode": "9243",
    "locationName": "Jonschwil",
    "country": "CH",
    "email": null,
    "phone": null,
    "dateOfBirth": null
  },
  "shippingAddress": null,
  "orderAmount": 1200
}

当我在调试时查看模型时,应该有所有必需的数据:

有人知道为什么会这样吗?

如果您需要更多代码或信息,请直接说出来,我会编辑问题。

终于找到错误了

问题是数据库中的企业名称为空,即使地址类型是企业地址。

感谢所有发表评论的人。