1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
- namespace FNZCM.ConHost
- {
- internal static class ExtensionMethods
- {
-
- public static void WriteTextUtf8(this HttpListenerContext context, string content, string contentType = Const.TextHtml)
- {
- var bytes = Encoding.UTF8.GetBytes(content);
- context.Response.ContentEncoding = Encoding.UTF8;
- context.Response.ContentType = contentType;
- if (true == context.Request.Headers["Accept-Encoding"]?.Contains("gzip"))
- {
- context.Response.AddHeader("Content-Encoding", "gzip");
- var memoryStream = new MemoryStream(bytes);
- var gZipStream = new GZipStream(context.Response.OutputStream, CompressionMode.Compress, false);
- memoryStream.CopyTo(gZipStream);
- gZipStream.Flush();
- }
- else
- {
- context.Response.OutputStream.Write(bytes);
- }
- }
- public static string ToMd5(this string s)
- {
- var bytes = Encoding.UTF8.GetBytes(s);
- var md5 = MD5.Create();
- var hash = md5.ComputeHash(bytes);
- var hex = string.Join("", hash.Select(p => p.ToString("x2")));
- return hex;
- }
- }
- }
|