添加自定义Get和Post支持
This commit is contained in:
parent
303530589c
commit
7275e8810e
42
Inotify/Sends/Products/HttpGetTemplate.cs
Normal file
42
Inotify/Sends/Products/HttpGetTemplate.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Inotify.Sends.Products
|
||||
{
|
||||
public class HttpGetAuth
|
||||
{
|
||||
[InputTypeAttribte(0, "URL", "请求地址", "https://api.day.app/token/{title}/{data}")]
|
||||
public string URL { get; set; }
|
||||
}
|
||||
|
||||
|
||||
[SendMethodKey("ADB11045-F2C8-457E-BF7E-1698AD37ED53", "自定义GET", Order = 4)]
|
||||
public class HttpGetTemplate : SendTemplate<HttpGetAuth>
|
||||
{
|
||||
public override HttpGetAuth Auth { get; set; }
|
||||
|
||||
public override bool SendMessage(SendMessage message)
|
||||
{
|
||||
var url = Auth.URL.Replace("{title}", message.Title).Replace("{data}", message.Data);
|
||||
var webRequest = WebRequest.Create(url);
|
||||
|
||||
if (webRequest != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
webRequest.GetResponse().GetResponseStream();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
77
Inotify/Sends/Products/HttpPostTemplate.cs
Normal file
77
Inotify/Sends/Products/HttpPostTemplate.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Inotify.Sends.Products
|
||||
{
|
||||
public class HttpPostAuth
|
||||
{
|
||||
[InputTypeAttribte(0, "URL", "请求地址", "https://api.day.app/token/{title}/{data}")]
|
||||
public string URL { get; set; }
|
||||
|
||||
[InputTypeAttribte(1, "Encoding", "Encoding", "utf-8|ascii|gbk")]
|
||||
public string Encoding { get; set; }
|
||||
|
||||
[InputTypeAttribte(2, "Data", "POST参数", @"{""msgid"":""123456"",""title"":""{title}"",""data"":""{data}""}")]
|
||||
public string Data { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
[SendMethodKey("A3C1E614-717E-4CF1-BA9B-7242717FC037", "自定义POST", Order = 5)]
|
||||
public class HttpPostTemplate : SendTemplate<HttpPostAuth>
|
||||
{
|
||||
public override HttpPostAuth Auth { get; set; }
|
||||
|
||||
public override bool SendMessage(SendMessage message)
|
||||
{
|
||||
if (Auth.Data == null)
|
||||
Auth.Data = "";
|
||||
|
||||
if (string.IsNullOrEmpty(Auth.Encoding))
|
||||
Auth.Encoding = "utf-8";
|
||||
|
||||
var url = Auth.URL.Replace("{title}", message.Title).Replace("{data}", message.Data);
|
||||
var webRequest = WebRequest.Create(url);
|
||||
|
||||
if (webRequest != null)
|
||||
{
|
||||
var data = Auth.Data.Replace("{title}", message.Title).Replace("{data}", message.Data);
|
||||
var bytes = Encoding.GetEncoding(Auth.Encoding).GetBytes(data);
|
||||
webRequest.Method = "POST";
|
||||
webRequest.ContentType = "application/x-www-urlencoded";
|
||||
webRequest.ContentLength = bytes.Length;
|
||||
var requestStream = webRequest.GetRequestStream();
|
||||
requestStream.Write(bytes, 0, bytes.Length);
|
||||
requestStream.Close();
|
||||
|
||||
try
|
||||
{
|
||||
webRequest.GetResponse().GetResponseStream();
|
||||
return true;
|
||||
}
|
||||
|
||||
catch(Exception ex)
|
||||
{
|
||||
WebException wex = (WebException)ex;
|
||||
var s = wex.Response.GetResponseStream();
|
||||
string ss = "";
|
||||
int lastNum;
|
||||
do
|
||||
{
|
||||
lastNum = s.ReadByte();
|
||||
ss += (char)lastNum;
|
||||
} while (lastNum != -1);
|
||||
s.Close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -96,10 +96,10 @@ namespace Inotify
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
#if !DEBUG
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseFileServer();
|
||||
#endif
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
|
Loading…
Reference in New Issue
Block a user