0
 var client = new HttpClient();
 client.DefaultRequestHeaders.Add("x-ms-version", "2016-05-31");
 var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]
 {
    new KeyValuePair<string, string>("api-version", "2016-08-01")
 });
 content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
 var response = client.PostAsync("https://management.azure.com/subscriptions/SuscriptionID/resourceGroups/Default-Web-SoutheastAsia/providers/Microsoft.Web/sites/MyAppName/stop?", content);

这就是我调用 Azure WebApp rest api 的方式,但我得到了状态码:BadRequest

4

1 回答 1

0

您的代码存在一些问题:

  • 你正在Azure Service Management APIAzure Resource Manager (ARM) API. 停止 Web 应用程序的 API 是资源管理器 API,因此您无需提供x-ms-version.
  • Authorization的请求中缺少标头。ARM API 请求需要授权标头。请参阅此链接了解如何执行 ARM API 请求:https ://docs.microsoft.com/en-us/rest/api/gettingstarted/ 。

基于这些,我修改了您的代码:

    static async void StopWebApp()
    {
        var subscriptionId = "<your subscription id>";
        var resourceGroupName = "<your resource group name>";
        var webAppName = "<your web app name>";
        var token = "<bearer token>";
        var url = string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}/stop?api-version=2016-08-01", subscriptionId, resourceGroupName, webAppName);
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
        var t = await client.PostAsync(url, null);
        var response = t.StatusCode;
        Console.WriteLine(t.StatusCode);
    }

请尝试使用此代码。假设您已获得正确的令牌,代码应该可以工作。

于 2017-01-18T14:23:39.097 回答