I need help migrating XML in a SOAP envelope to a third-party SOAP server. A third party provided xsd files for the incoming request and the outgoing response. I took these XSD files and created their C # classes using the xsd tool. My problem is that I need to wrap the serialized request with a SOAP envelope, and I don't know where to start. I was looking at Microsoft Web Service Enhancements 3, but this suggests that it is only for .net 2.0 and VS2005. I am using VS2012 and .net 4.5. In addition, I learned how to connect to the server using a web service, but it does not seem compatible and does not have WSDL .
The following is an example of what a SOAP server expects for an incoming request.
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <GetBasicData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="CRS610MI"> <CONO xmlns="">1</CONO> <CUNO xmlns="">12345</CUNO> </GetBasicData> </soap:Body> </soap:Envelope>
This is what a serialized XML string looks like.
<?xml version="1.0" encoding="utf-8"?> <GetBasicData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="CRS610MI"> <CONO xmlns="">1</CONO> <CUNO xmlns="">12345</CUNO> </GetBasicData>
The code I use for my web request and response.
Byte[] byteArray = System.Text.UTF8Encoding.UTF8.GetBytes(data); WebRequest webRequest = WebRequest.Create(@"http://myserver:8888"); webRequest.ContentLength = byteArray.Length; webRequest.ContentType = @"text/xml; charset=utf-8"; webRequest.Headers.Add("SOAPAction", @"http://schemas.xmlsoap.org/soap/envelope/"); webRequest.Method = "POST"; Stream requestStream = webRequest.GetRequestStream(); requestStream.Write(byteArray, 0, byteArray.Length); requestStream.Close(); requestStream.Dispose(); WebResponse webResponse = webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream); String line; while ((line = streamReader.ReadLine()) != null) { Debug.WriteLine(line); }
I tested my code by replacing my serialized string with text in an example file provided by a third party, and it worked as expected. I also took my serialized string and inserted the envelope text in the correct places, and it also worked, the web request went through, and I got the answer I was looking for. If you donβt enter the envelope text in my serialized string manually, what can I do. Should I imagine a method or class that takes care of this for me in a standard way?
source share