Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a body to a HttpWebRequest that is being used with the azure service mgmt api

How would i go about adding to the body of a HttpWebRequest?

The body needs to be made up of the following

<?xml version="1.0" encoding="utf-8"?>
<ChangeConfiguration xmlns="http://schemas.microsoft.com/windowsazure">
   <Configuration>base-64-encoded-configuration-file</Configuration>
   <TreatWarningsAsError>true|false</TreatWarningsAsError>
   <Mode>Auto|Manual</Mode>
</ChangeConfiguration>

Any help is much appreciated

like image 957
StevenR Avatar asked Feb 05 '12 21:02

StevenR


2 Answers

byte[] buf = Encoding.UTF8.GetBytes(xml);

request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = buf.Length;
request.GetRequestStream().Write(buf, 0, buf.Length);

var HttpWebResponse = (HttpWebResponse)request.GetResponse();
like image 121
L.B Avatar answered Oct 19 '22 22:10

L.B


Don't know about Azure, but here just the general outline to send data with a HttpWebRequest :

string xml = "<someXml></someXml>";
var payload = UTF8Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://foo.com");
request.Method = "POST";
request.ContentLength = payload.Length;
using(var stream = request.GetRequestStream())
stream.Write(payload, 0, payload.Length);

If you don't need a HttpWebRequest for some reason, using a WebClient for uploading data is much more concise:

using (WebClient wc = new WebClient())
{
    var result = wc.UploadData("http://foo.com", payload);
}
like image 6
BrokenGlass Avatar answered Oct 19 '22 23:10

BrokenGlass