如何使用补丁方法异步httpclient [英] How to Async httpclient with Patch Method

查看:84
本文介绍了如何使用补丁方法异步httpclient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用[此API]( https://docs.microsoft.com/zh-cn/rest/api/vsts/release/approvals/update ).下面是我的代码,但我收到了400个错误的请求.

I am trying to consume [this API] (https://docs.microsoft.com/en-us/rest/api/vsts/release/approvals/update). Below is my code, but i am getting 400 bad request.

HttpContent z = new StringContent("{\"status\": \"approved\",\"comments\": \"" + Request.QueryString["comment"].ToString() + "\"}", Encoding.UTF8, "application/json");                    

public static async  Task PatchAsync(Uri requestUri, HttpContent content)
{
    try
    {                 
        using (HttpClient client = new HttpClient())
        {
            var method = new HttpMethod("PATCH");
            var request = new HttpRequestMessage(method, requestUri)
            {
                Content = content
            };

            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "", "XXXXXXXXX"))));

            //using (HttpResponseMessage response = await client.PostAsync(requestUri, content))
            using (HttpResponseMessage response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                respApproval = responseBody;
            }
        }
    }
    catch (Exception ex)
    {
        respApproval = ex.ToString();
    }
}

推荐答案

由于您仅提供部分代码,因此我在下面发布了我的代码(可以成功更新批准)供您参考:

Since you only provide part of the code, I posted my code (which can update approvals successfully) below for your refernce:

public static async void ApproveRelease()
{
  try
  {
    var username = "alternate auth or PAT";
    var password = "password";
    string accountName = "https://account.visualstudio.com";
    string projectName = "projectname";
    int approvalid = id;
    var approveReleaseUri = "https://accountname.vsrm.visualstudio.com/projectname/_apis/release/approvals/approvlID?api-version=4.1-preview.3";

    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(
          System.Text.ASCIIEncoding.ASCII.GetBytes(
            string.Format("{0}:{1}", username, password))));

        var method = new HttpMethod("PATCH");
        string approvveReleaseMetaData = "{\"status\":\"approved\", \"comments\":\"Good to go\"}";
        var request = new HttpRequestMessage(method, string.Format(approveReleaseUri, accountName, projectName, approvalid, apiVersion))
        {
            Content = new StringContent(approvveReleaseMetaData, Encoding.UTF8, "application/json")
        };

        using (HttpResponseMessage response = client.SendAsync(request).Result)
        {
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
    }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }

}

通过引用博客使用ReleaseManagement REST API的.

注意:您只能更新状态为pending的发布批准.如果您尝试更新批准状态为approvedrejected的发布批准,您还将收到400 bad request响应.

Note: you can only update a release approval which status is pending. If you try to update a release approval which approval status is approved or rejected, you will also get the 400 bad request response.

这篇关于如何使用补丁方法异步httpclient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆