2012-02-28 18 views
0

私は学生で、ユーザーがAzureでホストされているサービスのインスタンス数を変更できるようにするアプリケーションを作成しようとしています。これは、サービス (http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx)の新しい構成ファイルをアップロードすることです。私の問題は、以下のコードでレスポンスを取得しようとすると「リモートサーバーがエラーを返しました:(403)Forbidden」というエラーが表示されることです。私はエラーが証明書とは何かである必要がありますが、 GETリクエストは正常に実行され、ここで使用するのと同じ証明書を使用して正しい応答が得られます。どんな助けも非常に高く評価されています.configは新しい設定ファイルです。Azureサービス管理のAPI設定を変更する

公共ボイドchangeConfiguration(文字列serviceNameは、文字列deploymentSlot、文字列の設定、文字列deploymentName)

{ 

     byte[] encodedConfigbyte = new byte[config.Length]; 
     encodedConfigbyte = System.Text.Encoding.UTF8.GetBytes(config); 
     string encodedConfig = Convert.ToBase64String(encodedConfigbyte); 

     Uri changeConfigRequestUri = new Uri("https://management.core.windows.net/" + subscriptionId + "/services/hostedservices/" + serviceName + "/deployments/" + deploymentName + "/?comp=config)"); 

     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(changeConfigRequestUri); 

     request.Headers.Add("x-ms-version", "2010-10-28"); 
     request.Method = "POST"; 

     string bodyText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
          "<ChangeConfiguration xmlns=\"http://schemas.microsoft.com/windowsazure" + ">" + "<Configuration>" + encodedConfig + "</Configuration>" + "<TreatWarningsAsError>false</TreatWarningsAsError>" + "<Mode>Auto</Mode>"+"</ChangeConfiguration>"; 

     byte[] buf = Encoding.UTF8.GetBytes(bodyText); 
     request.ContentType = "text/xml"; 
     request.ContentLength = buf.Length; 

     StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
     var data = Encoding.ASCII.GetBytes(buf.ToString()); 
     writer.Write(data); 
     writer.Flush(); 
     writer.Close(); 

     X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
     try 
     { 
      certStore.Open(OpenFlags.ReadOnly); 
     } 
     catch (Exception e) 
     { 
      if (e is CryptographicException) 
      { 
       Console.WriteLine("Error: The store is unreadable."); 
      } 
      else if (e is SecurityException) 
      { 
       Console.WriteLine("Error: You don't have the required permission."); 
      } 
      else if (e is ArgumentException) 
      { 
       Console.WriteLine("Error: Invalid values in the store."); 
      } 
      else 
      { 
       throw; 
      } 
     } 


     X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false); 
     certStore.Close(); 

     if (certCollection.Count == 0) 
     { 
      throw new Exception("Error: No certificate found containing thumbprint " + thumbprint); 
     } 

     X509Certificate2 certificate = certCollection[0]; 
     request.ClientCertificates.Add(certificate); 


     //Error occurs in line below 
     WebResponse response = (HttpWebResponse)request.GetResponse(); 
     try 
     { 
      response = request.GetResponse(); 
     } 
     catch (WebException e) 
     { 
      string test = e.Message; 
     } 

答えて

0

コンテンツタイプが間違っているように見えます。これはAPI呼び出しの要件の1つです。

あなたが持っている:

request.ContentType = "text/xml"; 

あなたが持っている必要があります。

request.ContentType = "application/xml"; 

はまた、あなたの代わりにあなたのX-MS-バージョンに "2010-10-28" を設定しているがその理由であります最新APIの "2011-08-01"です。

参考:http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx

編集:私はまた、あなたの体のデータを追加しているかと少し心配です。テキストをUTF8バイト配列に変換してから、バイト配列にtostringを実行し、それをASCIIバイト配列に再エンコードするようです。これは私にとって奇妙です。 StreamWriterのWrite(bodyText)メソッドに直接文字列を渡すことができるはずです。 APIは送信しているボディデータのデコード方法をAPIが知りません。

request.ContentType = "application/xml"; 

    using (var writer = new StreamWriter(request.GetRequestStream())) 
    { 
     writer.Write(bodyText); 
    } 
...このような何かを試してみてください
関連する問題