110,539
社区成员
发帖
与我相关
我的任务
分享
//监听按键信息
System.Threading.Timer threadTimer;
/// <summary>
/// 开启监听
/// </summary>
private void StartListen()
{
threadTimer = new System.Threading.Timer(new TimerCallback(CatchTelNumber), null, 1000, 500);
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://localhost:4529/login/");
listerner.Start();
}
/// <summary>
/// 获取按键信息
/// </summary>
/// <param name="state"></param>
private void CatchTelNumber(object state)
{
_th = new Thread(new ThreadStart(() =>
{
//等待请求连接
//没有请求则GetContext处于阻塞状态
lock (_lock)
{
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
string name = ctx.Request.QueryString["c_meb_no"];
if (name != null)
{
catchKey.Append(name);
SetBtnEnable(name);
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
writer.WriteLine("<html><head><title>WebServer</title></head><body>");
writer.WriteLine("<div>hello{0}</div>", name);
writer.WriteLine("</body></html>");
writer.Close();
ctx.Response.Close();
}
}
}
}));
_th.IsBackground = true;
_th.Start();
}
/// <summary>
/// 关闭窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Click(object sender, EventArgs e)
{
Cl = null;
Ph = null;
threadTimer.Dispose();
listerner.Stop();
this.Dispose();
}