Created
November 1, 2018 09:21
-
-
Save VatthanachaiW/c9ba20609a39708bc89cc5598a904b05 to your computer and use it in GitHub Desktop.
Notification Controller for recieved data from gitlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Mime; | |
using System.Text; | |
using System.Web.Http; | |
using Notifications.Models; | |
using log4net; | |
namespace Notifications.Controllers | |
{ | |
public class NotificationController : ApiController | |
{ | |
private readonly ILog _logger = | |
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// POST api/values | |
public void Post(PushEvent push) | |
{ | |
if (ModelState.IsValid) | |
{ | |
string token = "<Line Notify Token Here>"; | |
string lineuri = "https://notify-api.line.me/api/notify"; | |
string contentType = "application/x-www-form-urlencoded"; | |
try | |
{ | |
var requset = WebRequest.Create(lineuri); | |
var postdata = $"message={push}"; | |
var data = Encoding.UTF8.GetBytes(postdata); | |
requset.Method = WebRequestMethods.Http.Post; | |
requset.ContentType = contentType; | |
requset.ContentLength = data.Length; | |
requset.Headers.Add("Authorization", $"Bearer {token}"); | |
using (var stream = requset.GetRequestStream()) | |
{ | |
stream.Write(data, 0, data.Length); | |
var response = requset.GetResponse(); | |
var strResponse = new StreamReader(response.GetResponseStream()).ReadToEnd(); | |
} | |
} | |
catch (Exception exception) | |
{ | |
_logger.Error(exception); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment