【问题标题】:Azure Service Mgmt Api configuration change (400 Bad request error)Azure Service Mgmt Api 配置更改(400 Bad request 错误)
【发布时间】:2012-03-30 11:43:01
【问题描述】:

我正在尝试使用 azure 管理 API 上传配置文件。我收到 400 错误请求错误,我不知道为什么,有什么建议吗?

这里是更改配置操作的 API 文档。 http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx

这是我的代码。任何回复都非常感谢

 public void changeConfiguration(string serviceName, string deploymentSlot, string config, string deploymentName)
    {
        byte[] encodedConfigbyte = new byte[config.Length];
        encodedConfigbyte = System.Text.Encoding.ASCII.GetBytes(config);
        string temp = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(config));

        Uri changeConfigRequestUri = new Uri("https://management.core.windows.net/" + subscriptionId + "/services/hostedservices/" + serviceName + "/deploymentslots/" + 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>" + temp + "</Configuration>              </ChangeConfiguration>";

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

        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 ");
        }
        X509Certificate2 certificate = certCollection[0];
        request.ClientCertificates.Add(certificate);
        Stream dataStream = request.GetRequestStream();

        dataStream.Write(buf, 0, buf.Length);

        dataStream.Close();

            //Error occurs in the line below
            WebResponse response = (HttpWebResponse)request.GetResponse();

    }

}

【问题讨论】:

    标签: c# api post configuration azure


    【解决方案1】:

    服务器的响应没有正文吗?

    看起来,当您构建 URL 时,您在需要“deploymentSlot”的地方使用了“deploymentName”。会这样吗?

    【讨论】:

    • 感谢您的回复,我不确定回复的正文,但文档说我可以使用部署名称或部署槽
    • 但是如果你使用部署名称,那么 URL 需要看起来像这样:https://management.core.windows.net/&lt;subscription-id&gt;/services/hostedservices/&lt;service-name&gt;/deployments/&lt;deployment-name&gt;/?comp=config 你的 URL 看起来像使用部署槽的那个。
    • 很好,你是对的,我确实输入了错误的 URL,但是我已将其更改为 Uri changeConfigRequestUri = new Uri("https://management.core.windows.net/" + subscriptionId + "/services/hostedservices/" + serviceName + "/deployments/" + deploymentName + "/?comp=config");,但我仍然收到 400 bad request 错误
    • 你确定“deploymentName”是正确的吗? (我不相信门户会向您显示部署名称......如果您通过门户创建部署,我认为这是一个 GUID。)我认为 400 响应的正文会有所帮助。捕获异常并读取响应。
    • 你对部署名称是正确的,我输入了 id,我硬编码了正确的名称,但我仍然收到 400 错误请求,并且异常响应没有给我任何新信息
    猜你喜欢
    • 1970-01-01
    • 2015-03-08
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多