如何使用补丁方法异步httpclient

编程入门 行业动态 更新时间:2024-10-24 17:31:46
本文介绍了如何使用补丁方法异步httpclient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to consume [this API] (docs.microsoft/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 = "account.visualstudio"; string projectName = "projectname"; int approvalid = id; var approveReleaseUri = "accountname.vsrm.visualstudio/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的发布批准.如果您尝试更新批准状态为approved或rejected的发布批准,您还将收到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

本文发布于:2023-10-31 07:45:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   补丁   方法   httpclient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!