Reputation: 4066
I am trying to call a webservice by POST method. Input xml is rpc/literal soap message.
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()
Code throws an exception of "Remote server returned an error : 500" on the line objResponse = myRequest.GetResponse()
This is the actual soap response from web service:
<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>
This is working perfectly when I add a reference to this web service using visual studio (proxy class) but I do not need to use this method.
Please suggest if I am missing something!
Upvotes: 1
Views: 2632
Reputation: 4066
After spending some time on analysing and research of the wsdl of my service, I was able to find the fix for this issue.
We need to use CDATA (ie constant data) for all those xml nodes which are not direct exposed in WSDL and thus this tag should be sent as text to the service. This tag was under a parent tag which was defined in wsdl. I applied CDATA to send these information in my soap xml.
<PARENT><![CDATA[<CONTACT>abcd </CONTEXT>]]></PARENT>
I found some useful information about the error 500 - Remote server returned an error - This error is reproduced from service and there could be many reasons behind this. To get the exact reason for the error we require actual response from the service as fault code and fault string.
<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>
- Issue is from client ie client input.
<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"CONTACT"). Expected elements are (none) </faultstring>
This error suggests that we are passing blank uri for the element <CONTACT>
. uri here represents the namespace of the <CONTACT>
tag which should be available in the service wsdl of the corresponding operation. This element is referred here as LOCAL and as service did not have information about this tag in wsdl, it was trying to find the namespace for this element.
Hope this could help many more people facing this same problem.
Upvotes: 1