C# HttpClient.SendAsync 导致错误,无法发送具有此动词类型的内容正文

发布于 2025-01-09 01:25:13 字数 1286 浏览 1 评论 0原文

我收到错误无法发送具有此动词类型的内容正文。我正在从 C# VSTO 桌面应用程序调用 GET 端点。我做错了什么。

public static string GetCentralPath(LicenseMachineValidateRequestDTO licenseMachine)
{
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization =    new AuthenticationHeaderValue("Bearer", Properties.Settings.Default.Properties["JWT"].DefaultValue.ToString());
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{Constants.URL.APIBase}licensemachine/GetCentralPath"),
            Content = new StringContent(JsonConvert.SerializeObject(licenseMachine), Encoding.UTF8, "application/json"),
        };
        
        using (HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult()) // Causing ERROR
        {
            var result = GetStringResultFromHttpResponseMessage(response, true);
            if (string.IsNullOrEmpty(result))
                return null;
            return JsonConvert.DeserializeObject<string>(result);
        }
    }
}

终点如下所示:

[HttpGet("GetCentralPath")]
public async Task<IActionResult> GetCentralPath(LicenseMachineValidateRequestDTO dto)
{
    // Some code
}

I am getting error cannot send a content-body with this verb-type. I am calling a GET Endpoint from a C# VSTO desktop application. What am I doing wrong.

public static string GetCentralPath(LicenseMachineValidateRequestDTO licenseMachine)
{
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization =    new AuthenticationHeaderValue("Bearer", Properties.Settings.Default.Properties["JWT"].DefaultValue.ToString());
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri(
quot;{Constants.URL.APIBase}licensemachine/GetCentralPath"),
            Content = new StringContent(JsonConvert.SerializeObject(licenseMachine), Encoding.UTF8, "application/json"),
        };
        
        using (HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult()) // Causing ERROR
        {
            var result = GetStringResultFromHttpResponseMessage(response, true);
            if (string.IsNullOrEmpty(result))
                return null;
            return JsonConvert.DeserializeObject<string>(result);
        }
    }
}

The end point looks like the following:

[HttpGet("GetCentralPath")]
public async Task<IActionResult> GetCentralPath(LicenseMachineValidateRequestDTO dto)
{
    // Some code
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2025-01-16 01:25:13

修复操作,您无法使用 get 发送正文数据,请参阅此帖子
带有请求正文的 HTTP GET

[HttpPost("GetCentralPath")]
public async Task<IActionResult> GetCentralPath(LicenseMachineValidateRequestDTO dto)

并修复 request ,将 Method = HttpMethod.Get 替换为 Post,这就是生成错误的原因

var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri($"{Constants.URL.APIBase}licensemachine/GetCentralPath"),
            Content = new StringContent(JsonConvert.SerializeObject(licenseMachine), Encoding.UTF8, "application/json"),
        };

fix the action, you cannot send body data with get, see this post
HTTP GET with request body

[HttpPost("GetCentralPath")]
public async Task<IActionResult> GetCentralPath(LicenseMachineValidateRequestDTO dto)

and fix request , replace Method = HttpMethod.Get with Post, this is what generates an error

var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri(
quot;{Constants.URL.APIBase}licensemachine/GetCentralPath"),
            Content = new StringContent(JsonConvert.SerializeObject(licenseMachine), Encoding.UTF8, "application/json"),
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文