在HttpRequest中发送xml字符串

问题描述 投票:1回答:1

有一项服务接受数据作为Xml字符串。

我正在尝试将xml作为序列化字符串发送到此服务,但是我收到了400错误代码。我在做什么错?

我的要求课程:

public class AltoTrackClient
{
    //private static string _url = "http://ws4.altotrack.com";
    //private static string _action = "/WSPosiciones_WalmartMX/WSPosiciones_WalmartMX.svc?wsdl";

    public static string ProcessXml(string xmlString)
    {           
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://ws4.altotrack.com/WSPosiciones_WalmartMX/WSPosiciones_WalmartMX.svc?singleWsdl");

        byte[] requestInFormOfBytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
        request.Method = "POST";
        request.ContentType = "text/xml;charset=utf-8";
        request.ContentLength = requestInFormOfBytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(requestInFormOfBytes,0, requestInFormOfBytes.Length);
        requestStream.Close();
        string recievedResponse;            
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
            {
                recievedResponse = reader.ReadToEnd();
            }
        }
        return recievedResponse;
    }
}
c# httpwebrequest http-status-code-400
1个回答
1
投票

也许回答这个问题有点为时已晚,因为距提出该问题的日期已经超过1年零9个月了,但是如果有什么帮助,您可以从中找到一些解决方法的线索其他。

我最近从事的一个项目需要开发与Altotrack Web服务通信的流程。我试图向该Web服务发送一些请求,但最终我还是收到了与您发生的错误相同的错误400。至少可以说,他们的文档确实令人困惑且无益。

经过一些分析和尝试后,我发现必须如何设置POST请求的参数,以便Web服务以成功消息答复:

URL:http://ws4.altotrack.com/WSPosiciones_WalmartMX/WSPosiciones_WalmartMX.svc

标题: Content-Type : text/xml; charset=utf-8 SOAPAction : http://tempuri.org/IServicePositions/ProcessXML

请求有效负载:`

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
  <soapenv:Body>
    <tem:ProcessXML>
      <tem:xmlSerializado>
        &lt;registro&gt;
          &lt;systemUser&gt;AltoWS&lt;/systemUser&gt;
          &lt;password&gt;$Alt0WS&lt;/password&gt;
          &lt;movil&gt;
            &lt;proveedor&gt;My&lt;/proveedor&gt;
            &lt;dominio&gt;ABC1234&lt;/dominio&gt;
            &lt;nroSerie&gt;12345678&lt;/nroSerie&gt;
            &lt;fechaHoraEvento&gt;25-10-2019 12:34:56&lt;/fechaHoraEvento&gt;
            &lt;fechaHoraRecepcion&gt;25-10-2019 12:34:56&lt;/fechaHoraRecepcion&gt;
            &lt;latitud&gt;-22.215328&lt;/latitud&gt;
            &lt;longitud&gt;-49.6559497&lt;/longitud&gt;
            &lt;altitud&gt;666&lt;/altitud&gt;
            &lt;velocidad&gt;20&lt;/velocidad&gt;
            &lt;rumbo&gt;180&lt;/rumbo&gt;
            &lt;codigo&gt;0&lt;/codigo&gt;
          &lt;/movil&gt;
        &lt;/registro&gt;
      </tem:xmlSerializado>
    </tem:ProcessXML>
  </soapenv:Body>
</soapenv:Envelope>

`

当然,在请求有效负载中,我添加空格只是为了更清晰。如果您希望减小请求有效负载的大小,则可以毫无问题地删除它们。

[很久以前,我使用C#,但是我分析了代码的一部分,我说在ProcessXml方法中存储在参数xmlString中的值不是网络所期望的格式服务,因此应要求返回错误400。这也是我身上发生的事情。

我希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.