【问题标题】:Posting JSON data using HttpClient使用 HttpClient 发布 JSON 数据
【发布时间】:2016-06-27 05:21:48
【问题描述】:

我正在尝试以编程方式在 Visual Studio Team Services(以前的 Visual Studio Online)中创建服务挂钩订阅。在 Team Services 中创建项目时,将自动创建服务挂钩。以下是我的“工作项已创建”网络挂钩代码:

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

【问题讨论】:

  • 发布堆栈跟踪...
  • 在此处验证您的 Json 结构:jsonlint.com
  • 你在哪一行得到错误?
  • 同样的问题:你在哪一行得到错误?我复制了您的代码并对其进行了快速测试。可以成功创建 webhook。唯一的问题是“var appHref = content.links.applications.href;”因为响应消息中不存在“links.applications.href”。
  • 抱歉回复晚了..我收到错误@反序列化

标签: c# json httpclient azure-devops


【解决方案1】:

试试下面的代码-

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

其中 RootObject 是响应对象。

【讨论】:

  • RootObject 是您响应的类别。这是基于响应 json 格式创建的。您可以使用此链接 - json2csharp.com
猜你喜欢
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
相关资源
最近更新 更多