JSON HttpWebRequest 始终返回 400
我试图提出请求来测试人们是否应该能够按发票付款。
我以前从未使用 JSON 数据进行过 API 调用,所以我阅读了一些文档和堆栈溢出问题,并在这里找到了这个线程,这是非常有前途的: 如何使用 C# 将 JSON 发布到服务器?
我尝试执行此操作的代码是下面发布的代码:
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 bad request 错误。
错误消息显示: 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
}
当我在调试时查看模型时,应该有所有必需的数据:
有人知道为什么会发生这种情况吗?
如果您需要更多代码或信息,只需说出来,我就会编辑问题。
I tried to make a request to test if people should be able to pay on invoice.
I've never made an API call with JSON data before so i read through some documentations and stack overflow questions and found this thread right here, which was pretty promissing: How to post JSON to a server using C#?
The code where i attempt to do this is the one posted below:
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();
I tried to implement the answer to the question but sadly i always get a 400 bad request error when i try to send the httpRequest.
The error message says: System.Net.WebException: "The remoteserver responded with an error: (400) bad request"
Those are the details to the exception:
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.>.
That's the model i'm trying to serialize and add to the 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; }
}
That's the requested data in the API documentation:
{
"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
}
When i look at the model while debugging there should be all the required data:
Does someone know why this happens?
If you need more code or information just say it and i edit the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于发现了错误。
问题在于,即使地址类型是公司地址,数据库中的公司名称也为空。
感谢所有发表评论的人。
I finally found the error.
The problem was that the business name on the database was null even tho the address type was a business address.
Thanks for everyone who wrote a comment.