使用HttpClient发布JSON数据

时间:2016-06-27 05:21:48

标签: c# json httpclient azure-devops

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

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 the returned JSON object
                        var appHref = content.links.applications.href;
                    }
                }

运行此代码时,出现以下错误:Unexpected character encountered while parsing value: <. Path '',

任何人都可以帮我解决这个问题吗? 提前致谢。 堆栈跟踪是:

at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value)
   at TestApplication.Program.<SetServiceHook>d__1.MoveNext() in C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestApplication\TestApplication\Program.cs:line 94

1 个答案:

答案 0 :(得分:2)

尝试以下代码 -

var dataObjects = response1.Content.ReadAsStringAsync().Result;
var rootObj = JsonConvert.DeserializeObject<RootObject>(dataObjects);

其中 RootObject 是响应对象。