远程服务器返回错误 500 - 调用 soap webservice 时出错

Remote server returned an error 500 - Error while calling a soap webservice

我正在尝试通过 POST 方法调用网络服务。输入 xml 是 rpc/literal soap 消息。

    Dim myRequest As Net.HttpWebRequest
    Dim objResponse As Net.HttpWebResponse
    Dim result As String
    Dim data As Byte()
    Dim newStream As System.IO.Stream

    Dim strURL As String = "https://abxxxx"
    strXml.Load("D:\MyXml.xml")

    data = System.Text.Encoding.ASCII.GetBytes(strXml.InnerXml)
    myRequest = WebRequest.Create(strURL)
    myRequest.Method = "POST"
    myRequest.ContentType = "application/x-www-form-urlencoded"
    myRequest.ContentLength = data.Length
    myRequest.Timeout = 125000
    newStream = myRequest.GetRequestStream()
    newStream.Write(data, 0, data.Length)
    newStream.Close()
    objResponse = myRequest.GetResponse()
    Dim sr As IO.StreamReader = New IO.StreamReader(objResponse.GetResponseStream())
    strOutput = sr.ReadToEnd()
    'Close connections
    sr.Close()
    objResponse.Close()

代码在 objResponse = myRequest.GetResponse()

行抛出异常 "Remote server returned an error : 500"

这是来自 Web 服务的实际 soap 响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"CONTACT"). Expected elements are (none) </faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>

当我使用 visual studio(代理 class)添加对此 Web 服务的引用时,它工作得很好,但我不需要使用此方法。

如有遗漏请提出建议!

在花一些时间分析和研究我的服务的 wsdl 之后,我找到了解决这个问题的方法。

我们需要为所有未直接暴露在 WSDL 中的 xml 节点使用 CDATA(即常量数据),因此该标记应作为文本发送到服务。此标记位于 wsdl 中定义的父标记下。我应用 CDATA 在我的 soap xml.

中发送这些信息
<PARENT><![CDATA[<CONTACT>abcd </CONTEXT>]]></PARENT>

我找到了一些关于错误 500 的有用信息 - 远程服务器返回了一个错误 - 这个错误是从服务中重现的,这背后可能有很多原因。要获得错误的确切原因,我们需要服务的实际响应作为故障代码和故障字符串。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"CONTACT"). Expected elements are (none) </faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>

<faultcode>soap:Client</faultcode> - 问题来自客户端,即客户端输入。

<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"CONTACT"). Expected elements are (none) </faultstring>

此错误表明我们正在为元素 <CONTACT> 传递空白 uri。这里的uri代表<CONTACT>标签的命名空间,应该在相应操作的服务wsdl中可用。此元素在此处称为 LOCAL,并且由于服务在 wsdl 中没有关于此标记的信息,它正在尝试查找此元素的命名空间。

希望这可以帮助更多面临同样问题的人。