c# - 如何向 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 聊天资源并且不执行任何操作。
如果你能帮助我,那将是最好的,谢谢。
解决方案
如果您想从您的 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' })
推荐阅读
- sql - 如何在同一张表的同一列使用 WHERE 查询?
- haskell - 安装 Haskell 出现问题未能安装 ghc
- amazon-web-services - 我们可以将参数传递给 appsec.yml 挂钩 ApplicationStart 阶段吗?
- javascript - How get more info about these error like "Unspecified error" or "[object Object]"?
- python - Can I find out if one numpy vector appears as a slice of another?
- c++ - 如何评估程序的运行时间?
- python-3.x - Python comunication modbus tcp with plc saia sbc
- c++ - `std::shared_ptr`: 不是参数 `_Ty` 的有效模板类型参数
- c++ - 从流中读取图形到邻接表的向量
- mysql - GORM中的多个一对多关系