A simple way of creating and sending a Request on the Azure Management API

In an earlier post I’ve been talking about how one can communicate with the Azure Management API, however the examples were not completely clear for me, so I’ve made a try on my own, to be able to send a correct request on the REST API.
Here is what I do:
I read the certificate from the publishsettings file, and send it with the request as described in Programatically Adding Management Certificates to WebRequests using Azure Rest API. With the following snippet I’m actually trying to create a deployment for a virtual machine. The actual request body xml is in my case in the CreateDeployment.txt which looks similar to what you can see in the Post of Windows Azure Service Management API Cheat Sheet with PowerShell. I would like to point out the exception handling part, which is really useful when getting back only Bad Request messages.

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1
    class TestManagementApi
        private const string PublishSettingsFile = @"publishsettings file path";
        private const string SubscriptionId = "Subscription ID GUID";
        private const string XMsVersion = "2012-03-01";

        public static void CreateDeployment()
                var requestBodyContent = File.ReadAllText(@"CreateDeployment.txt");
                var requestBodyBytes = Encoding.UTF8.GetBytes(requestBodyContent);

                string requestUrl = string.Format(
                    "https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments", SubscriptionId, "hostedservicename");

                var req = (HttpWebRequest)WebRequest.Create(requestUrl);
                req.Method = "POST";
                req.ContentType = "application/xml";
                req.ContentLength = requestBodyBytes.Length;
                req.Headers.Add("x-ms-version", XMsVersion);
                using (Stream s = req.GetRequestStream())
                    s.Write(requestBodyBytes, 0, requestBodyBytes.Length);

                using (var resp = (HttpWebResponse)req.GetResponse())
                    var status = resp.StatusCode;
            catch (WebException webEx)
                var resp = webEx.Response;
                using (var respStream = resp.GetResponseStream())
                    if (respStream != null)
                        using (var sr = new StreamReader(respStream))
                            string s = sr.ReadToEnd();
                            throw new Exception(s);


        private static X509Certificate2 GetCertificate()
            XDocument xdoc = XDocument.Load(PublishSettingsFile);

            var base64Certificate =

            var certificate = new X509Certificate2(Convert.FromBase64String(base64Certificate));
            return certificate;

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , , , . Bookmark the permalink.

2 Responses to A simple way of creating and sending a Request on the Azure Management API

  1. Pingback: Working with Windows Azure Management REST API | Tamas' Blog

  2. Nithini says:

    I found an API manager product from WSO2 which includes samples and a guide on that. You can try out with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s