sending xml as http post

  • Thread starter Thread starter Maximus
  • Start date Start date
M

Maximus

Hi everyone,

I was wondering if anybody could help me with sending xml as part of an
http post. Any help will be greatly appreciated.
Thanks.
 
Maximus said:
Hi everyone,

I was wondering if anybody could help me with sending xml as part of
an http post. Any help will be greatly appreciated.

This sample method shows how to post an XML string "xml" to a web
application located at "url".

public void PostXml(string url, string xml) {
byte[] bytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format("POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}

Cheers,
 
I'm trying to do the same thing in vb.net.

Is there an equivalent vb.net example?

Thanks
Leslie



Maximus said:
Hi everyone,

I was wondering if anybody could help me with sending xml as part of
an http post. Any help will be greatly appreciated.

This sample method shows how to post an XML string "xml" to a web
application located at "url".

public void PostXml(string url, string xml) {
byte[] bytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format("POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}

Cheers,
 
Back
Top