LoginSignup
5
4

More than 3 years have passed since last update.

C#でWebHookを送信するプログラムを作ってみる

Posted at

DiscordでWebHookを使いたいから作りました。
取り敢えずプロジェクト作成してフォームとか全体図とか決めてみた
2019-09-03 (2).png
ダサいけどしょうがないね
label1 => コンテンツ
label2 => WebHookの名前
label3 => WebHookのアイコン
TextBoxは上から同じ感じにする(適当)

そんでusing ディレクティブを3個追加

MainForm.cs
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

次にDiscordでWebHookを作成
bandicam 2019-09-03 20-21-34-725.jpg
作ったらURLコピーしてWebHookのURLのとこにペースト

MainForm.cs
public static string WebHookUrl = "WebHookのURL";

private void WebHookBtn_Click(object sender, EventArgs e)
{
    HttpClient httpClient = new HttpClient();
    Dictionary<string, string> strs = new Dictionary<string, string>()
        {
            { "content", textBox1.Text },
            { "username", textBox2.Text },
            { "avatar_url", textBox3.Text }
        };
    TaskAwaiter<HttpResponseMessage> awaiter = httpClient.PostAsync(WebHookUrl, new 
    FormUrlEncodedContent(strs)).GetAwaiter();
    awaiter.GetResult();
}

こんな感じで完成
あとは個人で使えるかテストしてください

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4