2012-02-28 98 views
0

我是一名學生,我嘗試創建一個應用程序,允許用戶更改Azure上託管服務的實例數。這是我上傳一個新的服務配置文件 (http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx)。我的問題是,當我嘗試在下面的代碼中得到響應時,我一直在收到錯誤「遠程服務器返回錯誤:(403)禁止」。我認爲錯誤必須與證書有關,但我可以執行GET請求成功並使用我在此使用的相同證書獲得正確的響應。任何幫助非常讚賞.config是新的配置文件。Azure服務管理api更改配置

公共無效changeConfiguration(字符串服務名,串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調用的要求之一。

您有:

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可能不知道如何解碼您要發送的正文數據。

嘗試這樣的事情...

request.ContentType = "application/xml"; 

    using (var writer = new StreamWriter(request.GetRequestStream())) 
    { 
     writer.Write(bodyText); 
    }