[英]Why does my webrequest return an error 400 when I'm sure my XML is right?
I have tried sending data to an API through C#.我尝试通过 C# 将数据发送到 API。 It is my first time doing so and I unfortunately only get an error 400 when making my request.
这是我第一次这样做,不幸的是,我在提出请求时只收到错误 400。 I followed the exemple in this post to make my request, (using method C) but to no avail.
我按照这篇文章中的示例提出了我的请求,(使用方法 C)但无济于事。 Here's the code I have, which follow the exemple, save for creating the XML itself.
这是我的代码,它遵循示例,除了创建 XML 本身。 That said, for the purpose of my test, I extracted the result of my serialization into a variable.
也就是说,出于测试的目的,我将序列化的结果提取到一个变量中。 Her's the code:
她的代码:
var serializerRequest = new XmlSerializer(typeof(Request.ImportRequest));
var stream = new MemoryStream();
serializerRequest.Serialize(stream, importRequest);
stream.Flush();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(new string(Encoding.UTF8.GetChars(stream.GetBuffer())));
String test = new string(Encoding.UTF8.GetChars(stream.GetBuffer()));
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.ToString());
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
XmlSerializer serializerResponse = new XmlSerializer(typeof(Response.Result));
using (TextReader reader = new StringReader(new string(Encoding.UTF8.GetChars(stream.GetBuffer()))))
{
Response.Result result = (Response.Result)serializerResponse.Deserialize(reader);
return result;
}
}
return null;
And here's the XML that is being sent.这是正在发送的 XML。
<?xml version="1.0"?>
<ImportRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Login>
<Username>XXXXXXX</Username>
<Password>PPPPPPPPP</Password>
</Login>
<Test>True</Test>
<ClientID>9999</ClientID>
<Reference>8888888</Reference>
<ShipTo>
<Name>CLIENTNAME</Name>
<Address1>ADDRESS</Address1>
<Address2 />
<City>NAMUR</City>
<State>BE</State>
<PostalCode>5000</PostalCode>
<Country>BELGIUM</Country>
<Phone />
</ShipTo>
<ShippingLane>
<Region>LandMark Brussels</Region>
</ShippingLane>
<ShipMethod>LGINTSTD</ShipMethod>
<ItemsCurrency>EUR</ItemsCurrency>
<ProduceLabel>true</ProduceLabel>
<LabelFormat>ZPL</LabelFormat>
<LabelEncoding>ZPL</LabelEncoding>
<VendorInformation>
<VendorName>XXXXXXXXX</VendorName>
<VendorAddress1>ADDRESS</VendorAddress1>
<VendorCity>NAMUR</VendorCity>
<VendorState>BE</VendorState>
<VendorPostalCode>5000</VendorPostalCode>
<VendorCountry>Belgium</VendorCountry>
<VendorBusinessNumber>+3255555555</VendorBusinessNumber>
</VendorInformation>
<Packages>
<Package>
<Weight>10</Weight>
<DimensionsUnit>Kg</DimensionsUnit>
</Package>
</Packages>
</ImportRequest>
What bothers me the most is that when I put the XML into postman it work without an itch.最困扰我的是,当我将 XML 放入 postman 时,它工作起来没有任何痒感。 So I'm convinced my XML is working, and I followed the guide on how to make webrequest, but for some reason it doesn't seem to work.
所以我确信我的 XML 正在工作,我按照如何制作 webrequest 的指南进行操作,但由于某种原因它似乎不起作用。 I get an error 400 so I'm reaching the server.
我收到错误 400,所以我正在访问服务器。 It's certainly an issue with my code, but I can't find it.
这肯定是我的代码的问题,但我找不到它。 I would appreciate some help.
我会很感激一些帮助。 Thanks and have a nice day.
感谢,并有一个愉快的一天。
In this link you will found all result code在此链接中,您将找到所有结果代码
In this case error code 400 it refers to error client, that is "400 (Bad Request) 400 is the generic client-side error status, used when no other 4xx error code is appropriate. Errors can be like malformed request syntax, invalid request message parameters, or deceptive request routing etc.在这种情况下,错误代码 400 指的是错误客户端,即“400 (Bad Request) 400 是通用客户端错误状态,在没有其他 4xx 错误代码适用时使用。错误可能像格式错误的请求语法,无效请求消息参数,或欺骗性请求路由等。
The client SHOULD NOT repeat the request without modifications."客户端不应该在没有修改的情况下重复请求。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.