在.NET Core中工作但不在Framework 4.7.2中工作的HttpClient

时间:2018-11-16 05:32:56

标签: c# asp.net .net-core

我目前正在尝试向名为GET的网站发送Monday请求

使用.NET CORE,以下代码可以正常工作并按预期返回Json的内容,但是当我将相同的代码与Framework 4.7.2一起使用时,会出现此错误
ProtocolViolationException: Cannot send a content-body with this verb-type.

我的问题是,为什么它可以在.NET Core而不是标准控制台应用程序的Framework 4.7.2中工作。

    static void GetFoo()
    {
        Task task = Task.Run(async() =>
        {
            string url = "https://api.monday.com:443/v1/boards/foo/pulses.json?api_key=bar";
            using (HttpClient httpClient = new HttpClient())
            using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url))
            {
                httpRequestMessage.Headers.Add("Cach-Control",": max-age=0, private, must-revalidate");
                httpRequestMessage.Content = new StringContent("", System.Text.Encoding.UTF8, "application/json");
                HttpResponseMessage result = await httpClient.SendAsync(httpRequestMessage);
                string data = await result.Content.ReadAsStringAsync();

                // Just want to see a result for now, so writing it to the console.
                Console.WriteLine(JsonConvert.DeserializeObject(data).ToString());
            }
        });
        Task.WaitAll(task);
    }

0 个答案:

没有答案