如何向 Discord Webhooks C# 发送 POST 请求

Posted

技术标签:

【中文标题】如何向 Discord Webhooks C# 发送 POST 请求【英文标题】:How can I send a POST request to Discord Webhooks C# 【发布时间】:2019-10-02 19:34:21 【问题描述】:

所以我正在制作一个 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# 中发出 Web 请求时,我的首选库是 restsharp.org 您必须比“不起作用”更具体。这几乎意味着任何事情。究竟出了什么问题? 【参考方案1】:

如果你想从你的 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' )

【讨论】:

以上是关于如何向 Discord Webhooks C# 发送 POST 请求的主要内容,如果未能解决你的问题,请参考以下文章

discord bot c# 读取收到的直接消息

查找和删除 Discord webhook 消息

c#编程,通过向串口发数据的方式发送中文短信时,但中文显示乱码,如何软件解码?

当 Dropbox 不支持 Webhooks 时,Zapier 如何支持 Dropbox?

如何识别 Stripe webhooks 类型?

C#中如何向指定的窗口或程序发送按键?