Call a webhook via HTTP
Here we show how to call webhooks over HTTP, we can use this for our webhooks or those available on the internet with sites like IFTTT.First we do this with C# using a HTTP GET command. This can be used to read data, it could also be used to run a command but GET should be used when the state of the server is not affected by the call no matter how many times is called.
Again this example is available in the download.
Uri url = new Uri("http://your_website.com/webhooks.php");
//Use GET when the request does not change the state of the server or perform an action
WebClient wc = new WebClient();
wc.QueryString.Add("com", "inf");
wc.QueryString.Add("dev", "PC");
wc.QueryString.Add("key", "com");
string response = wc.DownloadString(url);
You can also POST a message and set data in the body of the message to be used by your webhook. POST Is used to perform an action where calling it again would not be desired.
//Use POST when the request causes a change in state or action to be performed
WebClient wc = new WebClient();
NameValueCollection data = new NameValueCollection
{
["Data1"] = "Some data",
["Data2"] = "Some more data"
};
wc.QueryString.Add("com", "msg");
wc.QueryString.Add("val", "Hello");
wc.QueryString.Add("dev", "PC");
byte[] response = wc.UploadValues(url, "POST", data);
string responseString = Encoding.UTF8.GetString(response);
To call a webhook on IFTTT you can use POST or GET, depending in how you have set up the webhook. Here is a GET call.
string YourKey = "put your IFTTT key in this string";
string YourEventName = "test";
//Call an IFTTT webhook
string IFTTT = $"https://maker.ifttt.com/trigger/{YourEventName}/with/key/{YourKey}";
Uri IFTTTurl = new Uri(IFTTT);
WebClient wc = new WebClient();
wc.QueryString.Add("value1", "Hello");
string response = wc.DownloadString(IFTTTurl);