首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure服务管理api更改配置

Azure服务管理api更改配置
EN

Stack Overflow用户
提问于 2012-02-29 02:41:11
回答 1查看 764关注 0票数 0

我是一名学生,我正在尝试创建一个应用程序,该应用程序允许用户更改Azure上托管服务的实例计数。这是通过上传服务的新配置文件(http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx)来完成的。我的问题是,当我试图在下面的代码中获得响应时,我一直收到错误消息"The remote server returned an error:(403) server“。我认为这个错误一定与证书有关,但我可以成功地执行get请求,并使用我在这里使用的相同证书获得正确的响应。任何帮助在很大程度上都是appreciated.config的新配置文件。

公共空配置( string serviceName,string deploymentSlot,string changeConfiguration,string deploymentName)

代码语言:javascript
复制
    {

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

回答 1

Stack Overflow用户

发布于 2012-04-03 04:30:00

看起来您的内容类型不正确。这是API调用的要求之一。

您必须:

代码语言:javascript
复制
request.ContentType = "text/xml";

你应该有:

代码语言:javascript
复制
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可能不知道如何解码您发送的正文数据。

试试这样的..。

代码语言:javascript
复制
    request.ContentType = "application/xml";

    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
         writer.Write(bodyText);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9487771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档