2015-12-21 115 views
15

我有很多Azure管理API使用下面的代码,除了GetRole for Virtual Machines。下面是该API调用的DOC:https://msdn.microsoft.com/en-us/library/azure/jj157193.aspx获取角色 - Azure API返回(400)错误请求

这里是我试图执行代码:

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

编辑:现在我有固定的网址,但我收到400 - 错误的请求。

+0

除了状态码之外,你能够在这里发布错误消息吗? – juvchan

+0

我收到的消息是:“远程服务器返回错误:(404)未找到。” – Jeremy

+0

400 - 错误的请求,您发送的HTTP消息格式错误。这可能有多种原因。您是否使用Web控制台调试器进行跟踪? – Ian

回答

0

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

相关问题