简体   繁体   English

当我确定我的 XML 正确时,为什么我的 webrequest 返回错误 400?

[英]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.

相关问题 反序列化我的XML时,出现错误消息XML无效 - When deserializing my XML, I'm getting an error that XML is invalid 为什么我的 HttpWebRequest 返回 400 Bad request? - Why Does my HttpWebRequest Return 400 Bad request? 我不确定我的游戏是否还在运作 - I'm not sure if my game is working yet 为什么我的C#客户端,POST到我的WCF REST服务,返回(400)错误请求? - Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request? 为什么我的 webrequest 行需要更新? (错误(426)需要升级) - Why does my webrequest line need updating? (error (426) Upgrade Required ) 为什么我的 WebAPI 出现 400 和 404 错误? - Why I am getting 400 and 404 error for my WebAPI? 尝试对GSM调制解调器软件进行编程时,为什么会出现问题? - Why does my GSM Modem Software get in the way when I'm trying to program it 当我传递HashSet时,为什么我的泛型类型方法认为T是system.object - Why does my generic type method think T is system.object when I'm passing in a HashSet 为什么退出我的应用程序时出现win32exception错误? - Why i'm getting a win32exception error when exiting my application? 我的 Web 服务操作调用仅在 IIS 中打开测试页面 - 我不确定如何调用该操作以返回我的 xml - My web service operation call only opens test page in IIS - I am not sure how to call the operation to return my xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM