15

除了用于虚拟机的 GetRole 之外,我有很多 Azure 管理 API 可以使用下面的代码。这是此 api 调用的文档:https ://msdn.microsoft.com/en-us/library/azure/jj157193.aspx

这是我要执行的代码:

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));
        }

编辑:我现在已修复 URL,但收到 400 - 错误请求。

4

1 回答 1

0

按照我之前发布的代码,我能够通过使用云服务名称作为部署来运行它。微软似乎在与他们的 MSDN 文档术语以及 Azure 门户上的术语保持一致时遇到了问题。

于 2016-02-29T21:24:06.533 回答