当前位置:  开发笔记 > 编程语言 > 正文

获取角色 - Azure API返回(400)错误请求

如何解决《获取角色-AzureAPI返回(400)错误请求》经验,为你挑选了0个好方法。

我有很多Azure管理API使用下面的代码,除了GetRole for Virtual Machines.这是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 locations = new List
            {
                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 - 错误请求.

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有