You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;
namespace Admin.Core.Socket.Plugins
{
/// <summary>
/// 支持GET、Post、PutDelete或者其他
/// </summary>
internal class MyHttpPlug : HttpPluginBase<HttpSocketClient>
{
protected override void OnGet(HttpSocketClient client, HttpContextEventArgs e)
{
if (e.Context.Request.UrlEquals("/success"))
{
//直接响应文字
e.Context.Response.FromText("Success").Answer();//直接回应
Console.WriteLine("处理完毕");
e.Handled = true;
}
else if (e.Context.Request.UrlEquals("/file"))
{
//直接回应文件。
e.Context.Response
.SetStatus()//必须要有状态
.FromFile(@"D:\System\Windows.iso", e.Context.Request);
}
else if (e.Context.Request.UrlEquals("/html"))
{
//回应html
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<!DOCTYPE html>");
stringBuilder.AppendLine("<html>");
stringBuilder.AppendLine("<head>");
stringBuilder.AppendLine("<meta charset=\"utf-8\"/>");
stringBuilder.AppendLine("<title>TouchSocket</title>");
stringBuilder.AppendLine("</head>");
stringBuilder.AppendLine("<body>");
stringBuilder.AppendLine("<div id=\"kuang\" style=\"width: 50%;height: 85%;left: 25%;top:15%;position: absolute;\">");
stringBuilder.AppendLine("<a id=\"MM\" style=\"font-size: 30px;font-family: 微软雅黑;width: 100%;\">王二麻子</a>");
stringBuilder.AppendLine("<input type=\"text\" id=\"NN\" value=\"\" style=\"font-size: 30px;width:100%;position: relative;top: 30px;\"/>");
stringBuilder.AppendLine("<input type=\"button\" id=\"XX\" value=\"我好\" style=\"font-size: 30px;width: 100%;position: relative;top: 60px;\" onclick=\"javascript;\"/>");
stringBuilder.AppendLine("</div>");
stringBuilder.AppendLine("</body>");
stringBuilder.AppendLine("</html>");
e.Context.Response
.SetStatus()//必须要有状态
.SetContentTypeByExtension(".html")
.SetContent(stringBuilder.ToString());
e.Context.Response.Answer();
}
base.OnGet(client, e);
}
protected override void OnPost(HttpSocketClient client, HttpContextEventArgs e)
{
if (e.Context.Request.UrlEquals("/uploadfile"))
{
try
{
if (e.Context.Request.TryGetContent(out byte[] bodys))//一次性获取请求体
{
return;
}
while (true)//当数据太大时,可持续读取
{
byte[] buffer = new byte[1024 * 64];
int r = e.Context.Request.Read(buffer, 0, buffer.Length);
if (r == 0)
{
return;
}
//这里可以一直处理读到的数据。
}
//下面逻辑是接收小文件。
if (e.Context.Request.ContentLen > 1024 * 1024 * 100)//全部数据体超过100Mb则直接拒绝接收。
{
e.Context.Response
.SetStatus("403", "数据过大")
.Answer();
return;
}
//此操作会先接收全部数据,然后再分割数据。
//所以上传文件不宜过大,不然会内存溢出。
var multifileCollection = e.Context.Request.GetMultifileCollection();
foreach (var item in multifileCollection)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append($"文件名={item.FileName}\t");
stringBuilder.Append($"数据长度={item.Length}");
client.Logger.Info(stringBuilder.ToString());
}
e.Context.Response
.SetStatus()
.FromText("Ok")
.Answer();
}
catch (Exception ex)
{
client.Logger.Exception(ex);
}
}
base.OnPost(client, e);
}
}
}