简体   繁体   中英

Make a post Request with azure new rest api (ressource manager)

I want to start my VM using the post Uri as described here https://msdn.microsoft.com/en-us/library/azure/mt163628.aspx

Since i don't have body in my request i get 403 frobidden. I can make a get Request without problem. Here is my code

public void StartVM()
    {
        string subscriptionid = ConfigurationManager.AppSettings["SubscriptionID"];
        string resssourcegroup = ConfigurationManager.AppSettings["ressourgroupename"];
        string vmname = ConfigurationManager.AppSettings["VMName"];
        string apiversion = ConfigurationManager.AppSettings["apiversion"];
        var reqstring = string.Format(ConfigurationManager.AppSettings["apirestcall"] + "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}/start?api-version={3}", subscriptionid, resssourcegroup, vmname, apiversion);
        string result = PostRequest(reqstring);

    }
public string PostRequest(string url)
    {
        string content = null;
        using (HttpClient client = new HttpClient())
        {
            StringContent stringcontent = new StringContent(string.Empty);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string token = GetAccessToken();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            HttpResponseMessage response = client.PostAsync(url, stringcontent).Result;
            if (response.IsSuccessStatusCode)
            {
                content = response.Content.ReadAsStringAsync().Result;
            }

        }
        return content;
    }

i've also tried this in the PostRequest

 var values = new Dictionary<string, string>
            {
               { "api-version", ConfigurationManager.AppSettings["apiversion"] }
            };

            var posteddata = new FormUrlEncodedContent(values);
 HttpResponseMessage response = client.PostAsync(url, posteddata).Result;

with url= string.Format(ConfigurationManager.AppSettings["apirestcall"] + "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}/start", subscriptionid, resssourcegroup, vmname);

I Get 400 Bad request

I found the solution. Needed to add role in Azure to allow starting/stopping the VM. That is why i received 4.3 forbidden.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM