Sending XML via HttpWebRequest

B

BuddyWork

Hello,

I want to send a XML message (string) to an ASP.net page,
I don't want to use WebService because the ASP.net page
has been developed by an external company, can you please
point me to the correct location for examples on how to
send XML messages to an ASP.net page using C#.

Thanks,
 
H

Hermit Dave

you want to send xml to aspx page but you do not want to use webservice.
Well in anycase if you want to post the data to a webpage you need to use
either QueryString or Form.
With XML i am afraid it can get pretty long and you could run out of max
querystring length.
in that case just htmlencode the xml and pass it as a form param.
Though to post to another aspx page (if you are using aspx as well) you will
have to rely on html form rather than server side form (so remove
runat=server from form in html view of designer)

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
C

Craig

I think something like this might work for you:


/// <summary>
/// Sends an xml document over http, and returns the xml server response
/// </summary
public XmlDocument SubmitDocument(XmlDocument xDoc, string URL)
{
try
{
// get the data from the xml document into a byte stream
Byte[] bdata = System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
// instantiate a web client
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] bresp;
// add appropriate headers
wc.Headers.Add("Content-Type","text/xml");
// send data to server, and wait for a response
bresp = wc.UploadData(URL, bdata);
// read the response
string resp = System.Text.Encoding.ASCII.GetString(bresp);
XmlDocument xresp = new XmlDocument();
xresp.LoadXml(resp);
// return the xml document response from the server
return xresp;
}
catch
{
// your error handler
SystemError();
}

}
 
J

Joerg Jooss

Craig said:
I think something like this might work for you:


/// <summary>
/// Sends an xml document over http, and returns the xml server
response /// </summary
public XmlDocument SubmitDocument(XmlDocument xDoc, string URL)
{
try
{
// get the data from the xml document into a byte stream
Byte[] bdata = System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
// instantiate a web client
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] bresp;
// add appropriate headers
wc.Headers.Add("Content-Type","text/xml");
// send data to server, and wait for a response
bresp = wc.UploadData(URL, bdata);
// read the response
string resp = System.Text.Encoding.ASCII.GetString(bresp);
XmlDocument xresp = new XmlDocument();
xresp.LoadXml(resp);
// return the xml document response from the server
return xresp;
}
catch
{
// your error handler
SystemError();
}

}

Just a note: Don't use ACSII encoding with XML. Use the specific encoding
your XML documents use, or UTF-8 as fallback. No non-7 bit character will
survive this torture otherwise ;-)

Cheers,
 
C

Craig

Joerg Jooss said:
Just a note: Don't use ACSII encoding with XML. Use the specific encoding
your XML documents use, or UTF-8 as fallback. No non-7 bit character will
survive this torture otherwise ;-)

Cheers,

You are, of course, correct, and thank you for pointing out my error.
That's what I get for throwing something together in a hurry.

Cheers,
Craig
 
J

Joerg Jooss

Craig said:
You are, of course, correct, and thank you for pointing out my error.
That's what I get for throwing something together in a hurry.

Never mind. Unfortunately, there are quite a few code samples on the web
employing this dangerous practice :-S

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top