[英]Azure service management api change configuration
我是一名学生,我试图创建一个允许用户更改Azure上托管服务的实例计数的应用程序。 通过上载服务的新配置文件(http://msdn.microsoft.com/zh-cn/library/windowsazure/ee460809.aspx),完成了此操作。 我的问题是,当我尝试在下面的代码中获取响应时,我不断收到错误“远程服务器返回错误:(403)禁止”,我认为错误一定与证书有关,但是我可以执行GET请求成功,并使用与此处相同的证书获得正确的响应。 非常感谢您的帮助。config是新的配置文件。
公共无效的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;
}
您的内容类型似乎不正确。 这是API调用的要求之一。
你有:
request.ContentType = "text/xml";
你应该有:
request.ContentType = "application/xml";
另外,是否有理由将x-ms-version设置为“ 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.