Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从httplclient调用中获取json?_C#_Sharepoint_Console Application_Httpclient - Fatal编程技术网

C# 如何从httplclient调用中获取json?

C# 如何从httplclient调用中获取json?,c#,sharepoint,console-application,httpclient,C#,Sharepoint,Console Application,Httpclient,我很难使用HTTPClient从sharepointrestapi获取json格式的数据。它以xml的形式返回数据。我正试图将标题设置为返回json,但显然我做得不对。 有人能帮忙吗 这是我正在测试的代码: using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new System.Net.Http.Head

我很难使用HTTPClient从sharepointrestapi获取json格式的数据。它以xml的形式返回数据。我正试图将标题设置为返回json,但显然我做得不对。 有人能帮忙吗

这是我正在测试的代码:

 using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization","NTLM");

                    //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    //client.DefaultRequestHeaders.Add("Content-Type", "application/json;odata=verbose");
                    HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, "http://sp2016/_api/lists/getbytitle('Holidays')/items");
                    msg.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await client.SendAsync(msg);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    //var result = await client.GetAsync(new Uri("http://sp2016/_api/lists/getbytitle('Holidays')/items"));

                    //if (result.IsSuccessStatusCode) { 
                    var content = await response.Content.ReadAsStringAsync();
                        var data = JsonConvert.DeserializeObject<Holidays>(content);
                }
使用(var-client=new-HttpClient())
{
client.DefaultRequestHeaders.Authorization=new System.Net.Http.Headers.authorizationHeaderValue(“授权”、“NTLM”);
//client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//Add(“内容类型”,“应用程序/json;odata=verbose”);
HttpRequestMessage msg=新的HttpRequestMessage(HttpMethod.Get)http://sp2016/_api/lists/getbytitle(“假期”)/项目);
msg.Content=newstringcontent(string.Empty,Encoding.UTF8,“application/json”);
HttpResponseMessage response=wait client.SendAsync(msg);
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”);
//var result=await client.GetAsync(新Uri(“http://sp2016/_api/lists/getbytitle("假期)/项目);;
//if(result.issucessStatusCode){
var content=await response.content.ReadAsStringAsync();
var data=JsonConvert.DeserializeObject(内容);
}

您通常应该在请求旁边包含一个
Accept
标题。一些具有多种输出格式的服务器将使用该标题来确定要返回的内容类型

var request = new HttpRequestMessage();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

您可以将
XML
响应转换为
Json
您尝试过吗?