15

I have got many for the Azure management APIs to work with the code below except for the GetRole for Virtual Machines. Here is the doc for this api call: https://msdn.microsoft.com/en-us/library/azure/jj157193.aspx

Here is the code that I am trying to execute:

static void Main(string[] args)
        {
            Program p = new Program();
            p.MakeRequest();
        }

        public void MakeRequest()
        {
            string strThumbprint = "{thumbprint}";
            X509Certificate2 certificate = GetStoreCertificate(strThumbprint);
            string strRequestURI = "https://management.core.windows.net/{subscription}/services/hostedservices/{cloud-service}/deployments/{deployment}/roles/{rolename}";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strRequestURI);
            request.ClientCertificates.Add(certificate);
            request.ContentType = "application/xml";
            request.Headers.Add("x-ms-version", "2015-04-01");
            try
            { 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine("Status Code: " + response.StatusCode.ToString());
            Stream receiveStream = response.GetResponseStream();
            Encoding encode = Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(receiveStream, encode);
            Console.WriteLine(readStream.ReadToEnd());
            response.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }

        }

        private static X509Certificate2 GetStoreCertificate(string thumbprint)
        {
            List<StoreLocation> locations = new List<StoreLocation>
            {
                StoreLocation.CurrentUser,
                StoreLocation.LocalMachine
            };

            foreach (var location in locations)
            {
                X509Store store = new X509Store("My", location);
                try
                {
                    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                    X509Certificate2Collection certificates = store.Certificates.Find(
                      X509FindType.FindByThumbprint, thumbprint, false);
                    if (certificates.Count == 1)
                    {
                        return certificates[0];
                    }
                }
                finally
                {
                    store.Close();
                }
            }
            throw new ArgumentException(string.Format(
              "A Certificate with Thumbprint '{0}' could not be located.",
              thumbprint));
        }

EDIT: I have now fixed the URL but I am receiving 400 - Bad Request.

Greg D
  • 41,086
  • 13
  • 81
  • 115
Jeremy
  • 414
  • 3
  • 17
  • Are you able to post the error message here besides only the status code? – juvchan Dec 21 '15 at 21:55
  • The message I receive is: "The remote server returned an error: (404) Not Found." – Jeremy Dec 21 '15 at 22:43
  • 400 - Bad request, The HTTP message you send is malformed. There could be various reason for this. Do you trace with the web console debugger? – Ian Dec 27 '15 at 05:40
  • 3
    Not sure if this is the problem but you shouldn't be sending a Content-Type header without a request body. Are you trying to get an XML response? If so set the Accept header instead. – Darrel Miller Dec 30 '15 at 00:41
  • I can't answer, but I can say that I have tried this code and it is working for me. Jeremy, are you trying it on a Virtual Machine - if you're trying it against a Cloud Service web role it will give you a 404 - otherwise it will give a 404 if the role name is wrong. Darrel Miller, it's working fine for me with the exact code in the original post (ie. the Content-Type header, while not necessarily correct, does not cause a 400). – SeanN Dec 31 '15 at 20:40
  • SeanN, I am not on a Virtual Machine. I am able to get some of the APIs to work (for instance, Get Deployment) but for some reason not Get Role. How were you able to get the above code to work? – Jeremy Jan 07 '16 at 17:57
  • It has been quite a while since I poked around the ASM API, but I'm sure that Bad Request is to do with the URI you're requesting. Are you sure your variables are correct? (this might also explain why @SeanN's code worked) – Michael B Jan 12 '16 at 06:58
  • Jeremy - when I asked if you were trying it on a Virtual Machine, I'm not referring to your computer, I mean the object you're querying in Azure. If you run this code and refer to a Cloud Service in the URL ({cloud-service} in the code above) you will get a 404. The code above will only work if you put a reference to an Azure Virtual Machine in place of {cloud-service} in the code above. – SeanN Jan 14 '16 at 22:41
  • Ok, I'm confused here. According to the documentation the URL should be this: "https://management.core.windows.net//services/hostedservices//deployments//roles/" So if I have a VM with the name of "MyVM" and a cloud service with the name "MyCloudService" is the following URL not correct: https://management.core.windows.net//services/hostedservices/MyCloudService/deployments/Production/roles/MyVM – Jeremy Jan 16 '16 at 02:53

1 Answers1

0

Following the code that I previously posted, I was able to get this to run by using the cloud service name as the deployment. It appears that Microsoft is having issues being consistent with their terminology for the MSDN documentation and what is on the Azure portal.

Jeremy
  • 414
  • 3
  • 17