I have the SOAP request in an XML file. I want to post the request to the web service in .net How to implement?
7 Answers
var uri = new Uri("http://localhost/SOAP/SOAPSMS.asmx/add");
var req = (HttpWebRequest) WebRequest.CreateDefault(uri);
req.ContentType = "text/xml; charset=utf-8";
req.Method = "POST";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", "http://localhost/SOAP/SOAPSMS.asmx/add");
var strSoapMessage = @"<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<soap:Body><add xmlns='http://tempuri.org/'><a>23</a><b>5</b></soap:Body>
</soap:Envelope>";
using (var stream = new StreamWriter(req.GetRequestStream(), Encoding.UTF8))
stream.Write(strSoapMessage);

- 300,895
- 165
- 679
- 742
-
1Hi what if i do not have SOAP ACTION value in my xml – Sep 19 '13 at 06:26
I've done something like this, building an xml request manually and then using the webrequest object to submit the request:
string data = "the xml document to submit";
string url = "the webservice url";
string response = "the response from the server";
// build request objects to pass the data/xml to the server
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
Stream post = request.GetRequestStream();
// post data and close connection
post.Write(buffer, 0, buffer.Length);
post.Close();
// build response object
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responsedata = response.GetResponseStream();
StreamReader responsereader = new StreamReader(responsedata);
response = responsereader.ReadToEnd();
The string variables at the start of the code are what you set, then you get a string response (hopefully...) from the server.

- 3,205
- 4
- 29
- 46

- 703
- 4
- 15
-
-
That'll be the request.ContentType, you should try 'text/xml' (I think) for a standard asp.net web service. – marcus.greasly Nov 13 '08 at 16:05
-
Its got working for the ASMX service.Now i want post the SOAP request for the WCF service.How to implement that. – Nov 14 '08 at 07:38
-
Using this code (after fixing the bug about two variables named "response") and changing the content type to "text/xml" worked for me. – dgundersen Jun 13 '12 at 23:08
This isn't the normal way. Usually you would use WCF or the older style web service reference to generate a proxy client for you.
However, what you need to do generally is use HttpWebRequest to connect to the URL and then send the XML in the body of the request.

- 87,846
- 14
- 132
- 192
Here's another example--this one in VB:
Dim manualWebClient As New System.Net.WebClient()
manualWebClient.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
' Note: don't put the <?xml... tag in--otherwise it will blow up with a 500 internal error message!
Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes( _
"<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">" & System.Environment.NewLine & _
" <soap12:Body>" & System.Environment.NewLine & _
" <Multiply xmlns=""http://cptr446.class/"">" & System.Environment.NewLine & _
" <x>5</x>" & System.Environment.NewLine & _
" <y>4</y>" & System.Environment.NewLine & _
" </Multiply>" & System.Environment.NewLine & _
" </soap12:Body>" & System.Environment.NewLine & _
"</soap12:Envelope>")
Dim bytRetData As Byte() = manualWebClient.UploadData("http://localhost/CPTR446.asmx", "POST", bytArguments)
MessageBox.Show(System.Text.Encoding.ASCII.GetString(bytRetData))

- 339
- 3
- 6
- 14
Sorry for bumping an old thread here's my solution to this
''' <summary>
''' Sends SOAP to a web service and sends back the XML it got back.
''' </summary>
Public Class SoapDispenser
Public Shared Function CallWebService(ByVal WebserviceURL As String, ByVal SOAP As String) As XmlDocument
Using wc As New WebClient()
Dim retXMLDoc As New XmlDocument()
wc.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
retXMLDoc.LoadXml(wc.UploadString(WebserviceURL, SOAP))
Return retXMLDoc
End Using
End Function
End Class

- 4,572
- 6
- 38
- 72
You need to post the data over HTTP. Use the WebRequest class to post the data. You will need to send other data with the post request to ensure you have a valid SOAP envelope. Read the SOAP spec for all of the details.

- 14,558
- 15
- 68
- 104
-
19lol, "Read the soap spec"... years later he comes back with a beard and gets put in a mental asylum. – John Lemp Nov 13 '08 at 15:01
-
The SOAP spec is relatively simple, it's the WS-whatever specifications that suck most. – Brian Lyttle Nov 13 '08 at 15:07