- //log配置
- Config cfg = new Config();
- //HTTP监听
- private HttpListener listeren = new HttpListener();
- public QRcode()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 获取启动参数
- /// </summary>
- /// <param name="args"></param>
- public void ShowEx(string[] args)
- {
- Args.RunningArgs = args;
- }
- /// <summary>
- /// 开启监听
- /// </summary>
- private void Init()
- {
- try
- {
- //指定身份验证 Anonymous匿名访问
- listeren.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
- //创建IP地址
- IPAddress address = IPAddress.Parse(Args.RunningArgs[0]);
- listeren.Prefixes.Add("http://" + address + ":" + Args.RunningArgs[1] + "/");
- listeren.Start();
- Thread threadlistener = new Thread(new ThreadStart(ThreadStartListener));
- threadlistener.Start();
- //MessageBox.Show("监听成功");
- }
- catch (Exception ex)
- {
- cfg.Logs.Add(new LogClass { LogStr = "HttpListener error", ExInfo = ex });
- }
- }
- /// <summary>
- /// 监听连接线程
- /// </summary>
- private void ThreadStartListener()
- {
- try
- {
- while (true)
- {
- // 注意: GetContext 方法将阻塞线程,直到请求到达
- HttpListenerContext context = listeren.GetContext();
- // 取得请求对象
- HttpListenerRequest request = context.Request;
- //响应
- Writer("ok", context);
- }
- }
- catch (Exception ex)
- {
- cfg.Logs.Add(new LogClass { LogStr = "HttpListener error", ExInfo = ex });
- }
- }
- /// <summary>
- /// 响应内容
- /// </summary>
- /// <param name="str"></param>
- /// <param name="context"></param>
- public void Writer(string str, HttpListenerContext context)
- {
- HttpListenerRequest request = context.Request;
- Console.WriteLine("{0} {1} HTTP/1.1", request.HttpMethod, request.RawUrl);
- Console.WriteLine("Accept: {0}", string.Join(",", request.AcceptTypes));
- Console.WriteLine("Accept-Language: {0}",
- string.Join(",", request.UserLanguages));
- Console.WriteLine("User-Agent: {0}", request.UserAgent);
- Console.WriteLine("Accept-Encoding: {0}", request.Headers["Accept-Encoding"]);
- Console.WriteLine("Connection: {0}",
- request.KeepAlive ? "Keep-Alive" : "close");
- Console.WriteLine("Host: {0}", request.UserHostName);
- Console.WriteLine("Pragma: {0}", request.Headers["Pragma"]);
- // 取得回应对象
- HttpListenerResponse response = context.Response;
- // 构造回应内容
- string responseString = str;
- // 设置回应头部内容,长度,编码
- response.ContentLength64
- = System.Text.Encoding.UTF8.GetByteCount(responseString);
- response.ContentType = "text/html; charset=UTF-8";
- // 输出回应内容
- System.IO.Stream output = response.OutputStream;
- System.IO.StreamWriter writer = new System.IO.StreamWriter(output);
- writer.Write(responseString);
- // 必须关闭输出流
- writer.Close();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- try
- {
- #region 开启日志
- string logPath = AppDomain.CurrentDomain.BaseDirectory + "log4net.config";
- LogHelper.SetConfig(new FileInfo(logPath));
- new Entity.LogClass().StartLogThread(ref cfg);
- cfg.Logs.Add(new LogClass { LogStr = "----------------------------------------------------------------------------------------------" });
- #endregion
- //开启监听
- Init();
- }
- catch (Exception ex)
- {
- cfg.Logs.Add(new LogClass { LogStr = "Load parameter error", ExInfo = ex });
- }
- }
复制代码
|