简体   繁体   中英

How can I send a POST request to Discord Webhooks C#

So I'm making a FiveM resource, that gets a message and then sends it to Discord channel. I'm need it to be server-sided script.

I've tried using WebClient, Flurl.Http and some async, await stuff, but none of them work

I have tried this 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);
             }

I have tried using the async, await with 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();

And for the final time, I have tried using Flurl.Http

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

Flurl.Http when using returns, that the dependency can't be found and the others just lag the entire FiveM chat resource and doesn't do anything.

If you could help me, it would be the best, thank you.

如果您想从您的 FiveM 服务器向您的 Discord 网络挂钩发送消息,请尝试使用此本机功能。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM