首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取角色- Azure API返回(400)坏请求

获取角色- Azure API返回(400)坏请求
EN

Stack Overflow用户
提问于 2015-12-21 21:08:38
回答 1查看 1.6K关注 0票数 15

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

下面是我正在尝试执行的代码:

代码语言:javascript
复制
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 -坏的请求。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-29 21:24:06

按照我之前发布的代码,我可以使用云服务名称作为部署来运行它。看来,微软存在着与MSDN文档的术语以及Azure门户上的内容相一致的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34404353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档