小编Anj*_*ha 的帖子

使用HttpClient发布JSON数据

我正在尝试以编程方式在Visual Studio Team Services(以前的Visual Studio Online)中创建服务挂钩订阅.在Team Services中创建项目时,将自动创建服务挂钩.以下是我的'workitem created'web钩子的代码:

using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

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

                    var request = new 
                    {
                        publisherId = "tfs",
                        eventType= " workitem.created",
                        resourceVersion= "1.0",
                        consumerId= "webHooks",
                        consumerActionId= "httpRequest",
                        publisherInputs= new {      
                            projectId= "test123",
                        }, 
                        consumerInputs= new {
                            url = "https://mydomain/api/ServiceHook/SaveWorkItem"
                        }
                    };


                    var response = client.PostAsync("https://mydomain/DefaultCollection/_apis/hooks/subscriptions",
                        new StringContent(JsonConvert.SerializeObject(request).ToString(),
                            Encoding.UTF8, "application/json"))
                            .Result;

                    if (response.IsSuccessStatusCode)
                    {
                        dynamic content = JsonConvert.DeserializeObject(
                            response.Content.ReadAsStringAsync()
                            .Result);


                        // Access variables from …
Run Code Online (Sandbox Code Playgroud)

c# json httpclient azure-devops

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

azure-devops ×1

c# ×1

httpclient ×1

json ×1