gpt4 book ai didi

c# - 使用 HttpClient 发布 JSON 数据

转载 作者:太空狗 更新时间:2023-10-29 23:37:13 24 4
gpt4 key购买 nike

我正在尝试以编程方式在 Visual Studio Team Services(以前是 Visual Studio Online)中创建服务 Hook 订阅。在 Team Services 中创建项目时,将自动创建服务 Hook 。以下是我的“workitem created”网络 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

最佳答案

试试下面的代码-

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

其中 RootObject 是响应对象。

关于c# - 使用 HttpClient 发布 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38046407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com