首页 > 解决方案 > 如何向 Discord Webhooks C# 发送 POST 请求

问题描述

所以我正在制作一个 FiveM 资源,它会收到一条消息,然后将其发送到 Discord 频道。我需要它是服务器端脚本。

我尝试过使用 WebClient、Flurl.Http 和一些异步、等待的东西,但它们都不起作用

我试过这个 WebClient

using (var wb = new WebClient())
             {
                 string content = message;
                 string username = "" + name + "(ID: )" + id;

                 var data = new NameValueCollection();
                 data["username"] = username;
                 data["content"] = content;

                 var response = wb.UploadValues(discordURL, "POST", data);
                 string responseInString = Encoding.UTF8.GetString(response);
                 Debug.WriteLine(responseInString);
             }

我尝试过使用 async,await 和 Dictionary

var values = new Dictionary<string, string>
            {
               { "username", name + " ID: " + id },
               { "content", message }
            };

            var content = new FormUrlEncodedContent(values);

            var response = await client.PostAsync(discordURL, content);

            var responseString = await response.Content.ReadAsStringAsync();

最后一次,我尝试使用 Flurl.Http

var responseString = await discordURL
            .PostUrlEncodedAsync(new { username = name + " ID: " + id, content = message })
            .ReceiveString();

Flurl.Http 使用返回时,找不到依赖项,而其他依赖项只是滞后于整个 FiveM 聊天资源并且不执行任何操作。

如果你能帮助我,那将是最好的,谢谢。

标签: c#discordwebhooks

解决方案


如果您想从您的 FiveM 服务器向您的 discord web-hook 发送消息,请尝试使用此本机功能。

PerformHttpRequest('discord web-hook link', function(Error, Content, Head) end, 'POST', json.encode({username = 'bot user name', content = 'message'}), { ['Content-Type'] = 'application/json' })

推荐阅读