Writing SOAP request from scratch
I had to test a few web services today and the test data was in the form of several XML files containing the actual SOAP envelope. I googled to see if I could find a simple enough tool on the web with no success. Eventually, I had to write my own
.
Here is a bit of code that may come in handy to someone someday -
HttpWebRequest request = (HttpWebRequest)
HttpWebRequest.Create(url);
String xmlString = txtInput.Text;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytesToWrite = encoding.GetBytes(xmlString);
request.Method = "POST";
request.ContentLength = bytesToWrite.Length;
request.Headers.Add("SOAPAction: \"http://localhost/XXX/ActionName\""); //You need to change this
request.ContentType = "text/xml; charset=utf-8";
Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
The code is actually quite straight forward. I create a HttpWebRequest and set some basic settings such as the content length and type. The one key thing is to add the SOAPAction in the http’s header. Once that is done, all we need to do is send the request to the url.

Hi there I have a similar problem here and I am trying to do the exact same. I’ve used your code but I have no idea what to substitute request.Headers.Add(“SOAPAction: \”http://localhost/XXX/ActionName\”"); by…
do you have any ideas…?
If you have an asmx file, open it and click on the operation – You will find the SOAPAction parameter there…
Thank you. That was very useful
Thanks, very usefull!
How can I retrieve soap request for a web method on a button click. I need to display it in a textbox.
Is there anyway of creating this same setup through WCF without using svcutil?
ROCKS!!!! Saved me a lot of time
The only thing you might want to indciate is the needed namespaces (for the un-initiated like myself.) Eg:
using System;
using System.Net; // added for HttpWebRequest/Response
using System.IO; // needed for Stream type
using System.Text; // needed for ASCIIEncoding
Came in very handy today. Needed to specify the SoapAction. Many thanks.
I can use this code to send xml message soap to url, not is web service the target.
i create a simple asmx web service C# that has a method that returns an int.
However, the java client while calling the web service added the following
They added the following to the request headers:
post.addRequestHeader(“Authorization”, “BASIC ” + getUserEncoding());
post.addRequestHeader(“Content-type”, “text/xml; charset=UTF-8″);
post.addRequestHeader(“SOAPAction”, “initiate”);
and they get the below error.
what should be done at my end to ensure that the error goes away.
Thank you very much… it was very useful..
HttpWebRequest request = (HttpWebRequest)
HttpWebRequest.Create(“http://btdevomneapp.btdev.com:12005/Services/VendorAPI.svc”);
String xmlString = “http://tempuri.org/IVendorAPI/EarlyCheckin93D3368A-C3B0-42CD-977E-D97D3B388D9F”;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytesToWrite = encoding.GetBytes(xmlString);
request.Method = “POST”;
request.ContentLength = bytesToWrite.Length;
request.Headers.Add(“SOAPAction: \”http://tempuri.org/IVendorAPI/EarlyCheckin\”"); //You need to change this
request.ContentType = “text/xml; charset=utf-8″;
Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
—————————————————-
I’m getting “The remote server returned an error: (400) Bad Request.” after executing.
Any ideas why i’m getting this exception.
thanks in advance.
Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
I too am an aspiring blog blogger but I’m still new to everything. Do you have any recommendations for newbie blog writers? I’d really appreciate it.
I simply couldn’t depart your web site before suggesting that I actually enjoyed the standard info a person provide for your guests? Is going to be back regularly to inspect new posts
Nice replies in return of this matter with genuine arguments and telling the
whole thing concerning that.
Your own post features proven beneficial to me. It’s really useful and
you are naturally quite well-informed in this field. You have got opened
our face to be able to numerous views on this subject together with intriguing,
notable and sound content material.
Wonderful work! That is the type of info that are meant to
be shared across the internet. Disgrace on the seek engines for no longer positioning this publish
upper! Come on over and discuss with my site . Thank you
=)
Raw organic Argan oil helps replenish your skin and makes you
look younger and rejuvenated almost instantly.
I am always advocating to drink more orange juice and eat fewer pastries around my home.
It may be applied on the hair of men who are suffering from
hair thinning.
[…] http://blogesh.wordpress.com/2007/03/01/writing-soap-request-from-scratch/ […]
请问如何用c#直接发送soap请求 | 技术控 said this on May 21, 2013 at 2:37 am |