| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.Eventing.Reader;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace HXX.Scanner.Biz
- {
- public class self_http_manager
- {
- private static self_http_manager _instance;
- private static string HostURL = "http://localhost:9998/";
- public static self_http_manager Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new self_http_manager();
- }
- return _instance;
- }
- }
- private System.Net.HttpListener _listener = null;
- public bool startSta = false;
- /// <summary>
- /// 启动
- /// </summary>
- //public void Start(string ip, int port)
- public void Start()
- {
- Stop();
- List<string> httpPrefixes = new List<string>();
- //httpPrefixes.Add("http://" + ip + ":" + port + "/" + "UpVmsRecord/");
- //httpPrefixes.Add("http://" + ip + ":" + port + "/");
- httpPrefixes.Add(HostURL);
- new Thread(new ThreadStart(delegate
- {
- _listener = new HttpListener();
- while (true)
- {
- try
- {
- _listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
- //_listener.Prefixes.Add(httpPrefixes0);
- //_listener.Prefixes.Add(httpPrefixes1);
- if (httpPrefixes != null)
- {
- foreach (string url in httpPrefixes)
- {
- _listener.Prefixes.Add(url);
- }
- }
- _listener.Start();
- }
- catch (Exception ex)
- {
- startSta = false;
- break;
- }
- //线程池
- int minThreadNum;
- int portThreadNum;
- int maxThreadNum;
- ThreadPool.GetMaxThreads(out maxThreadNum, out portThreadNum);
- ThreadPool.GetMinThreads(out minThreadNum, out portThreadNum);
- //ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), x);
- try
- {
- while (true)
- {
- startSta = true;
- //等待请求连接
- //没有请求则GetContext处于阻塞状态
- HttpListenerContext ctx = _listener.GetContext();
- ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc), ctx);
- }
- }
- catch
- {
- startSta = false;
- }
- }
- })).Start();
- }
- /// <summary>
- /// 停止
- /// </summary>
- public void Stop()
- {
- if (_listener != null)
- {
- _listener.Stop();
- _listener.Close();
- _listener = null;
- }
- }
- /// <summary>
- /// 任务进
- /// </summary>
- /// <param name="obj"></param>
- void TaskProc(object obj)
- {
- HttpListenerContext ctx = (HttpListenerContext)obj;
- try
- {
- var url = ctx.Request.Url.AbsoluteUri;
- //Stream stream = ctx.Request.InputStream;
- //System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8);
- //if (url.Contains("UpVmsRecord"))
- //{
- // string body = reader.ReadToEnd();
- // ////这里的body就是客户端发过来的数据
- // //var upRecord = Newtonsoft.Json.JsonConvert.DeserializeObject<UpEventRecord>(body);
- // //if (upRecord != null)
- // //{
- // // Form1._Instance.InsertRecord(upRecord);
- // //}
- //}
- //stream.Close();
- ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
- ctx.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
- ctx.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type,Authorization,X-Requested-With");
- ctx.Response.AddHeader("Access-Control-Max-Age", "3600");
- var bytes = bizWork(url);
- ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
- ctx.Response.Close();
- ctx = null;
- }
- catch (Exception ex)
- {
- System.Console.WriteLine(ex.ToString());
- }
- }
- private byte[] bizWork(string url)
- {
- byte[] bytes;
- try
- {
- var path = url.Replace(HostURL, "");
- path = config_manager.Get("dataDir") + "/" + path;
- if (File.Exists(path))
- {
- using (FileStream fileStream = new FileStream(path, FileMode.Open))
- {
- bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, 0, bytes.Length);
- }
- }
- else
- {
- bytes = Encoding.UTF8.GetBytes("File Not Found");
- }
- }
- catch (Exception ee)
- {
- bytes = Encoding.UTF8.GetBytes(ee.Message);
- }
- return bytes;
- }
- public static string get_url(string file_full_name)
- {
- var result = HostURL + file_full_name.Replace(config_manager.Get("dataDir"), "").Replace(@"\", "/");
- return result;
- }
- }
- }
|