2015-12-21 20 views
15

仮想マシンのGetRoleを除いて、Azure管理APIが以下のコードで動作するようになっています。ここでは、このAPI呼び出しのためのドキュメントは、次のとおりです。ここでhttps://msdn.microsoft.com/en-us/library/azure/jj157193.aspxGet Role - Azure API returns(400)Bad Request

は、私が実行しようとしているコードは次のとおりです。

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を受け付けております - 不正な要求を。

+0

ステータスコードだけでなく、ここにエラーメッセージを投稿できますか? – juvchan

+0

受信したメッセージは次のとおりです。「リモートサーバーからエラーが返されました:(404)見つかりません」 – Jeremy

+0

400 - リクエストが正しくありません。送信したHTTPメッセージの形式が正しくありません。これにはさまざまな理由があります。 Webコンソールデバッガでトレースしますか? – Ian

答えて

0

以前に投稿したコードに続いて、クラウドサービス名をデプロイメントとして使用してこれを実行することができました。マイクロソフトでは、MSDNのドキュメントの用語とAzureポータルの用語との整合性に問題があるようです。

関連する問題