Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Patch方法异步httpclient_C#_Azure Devops - Fatal编程技术网

C# 如何使用Patch方法异步httpclient

C# 如何使用Patch方法异步httpclient,c#,azure-devops,C#,Azure Devops,我正在尝试使用[此API]()。下面是我的代码,但我收到了400个错误的请求 HttpContent z = new StringContent("{\"status\": \"approved\",\"comments\": \"" + Request.QueryString["comment"].ToString() + "\"}", Encoding.UTF8, "application/json"); public static async T

我正在尝试使用[此API]()。下面是我的代码,但我收到了400个错误的请求

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();
    }
}

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

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());
  }

}
通过引用博客


注意:您只能更新状态为
pending
的发布批准。如果您尝试更新批准状态为
approved
rejected
的发布批准,您还将得到
400错误请求
响应。

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

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());
  }

}
通过引用博客


注意:您只能更新状态为
pending
的发布批准。如果您尝试更新批准状态为
approved
rejected
的发布批准,您还将得到
400错误请求
响应。

请求URI的值是多少?您能否在fiddler中看到请求的主体(内容)是一个合适的json字符串。@Levinija requestUri应该是“System.Uri=new System.Uri”(rmdomain+“projectname”+“/\u api/release/approvals/“+”releaseid”+“?api version=4.1-preview.3”)。如果您有“releaseid”,则需要为Approval设置一个变量。也许你只需要删除引号(如果releaseid是你的approvalId变量的名称)。我刚刚修复了这个问题,正要回复,看到了你的评论@levinja。这就是我传递releaseid而不是approvalid的问题。Marina Liu的便条让我想到了错误的参数。非常感谢。
requestUri
的值是什么?你能在fiddler中看到你的请求(内容)的主体是一个合适的json字符串吗。@Levinija requestUri应该是“System.Uri Uri=new System.Uri”(rmdomain+“projectname”+“/\u api/release/approvals/”+“releaseid”+“api version=4.1-preview.3”),其中有“releaseid”您需要为approvalId设置一个变量。也许你只需要删除引号(如果releaseid是你的approvalId变量的名称)。我刚刚修复了这个问题,正要回复,看到了你的评论@levinja。这就是我传递releaseid而不是approvalid的问题。Marina Liu的便条让我想到了错误的参数。非常感谢。感谢“使用ReleaseManagement REST API”链接的帮助。感谢“使用ReleaseManagement REST API”链接的帮助。