Get Role - Azure API returns (400) Bad Request
Asked Answered
M

1

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.

Mount answered 21/12, 2015 at 21:8 Comment(9)
Are you able to post the error message here besides only the status code?Hive
The message I receive is: "The remote server returned an error: (404) Not Found."Mount
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?Shenika
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.Ditch
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).Flyman
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?Mount
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)Lambency
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.Flyman
Ok, I'm confused here. According to the documentation the URL should be this: "management.core.windows.net/<subscription-id>/services/…>" 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: management.core.windows.net/<subscription-id>/services/…Mount
M
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.

Mount answered 29/2, 2016 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.