public class NotifyCallbackHelper
{
private string callBackUrl = string.Empty;
byte[] buffer = new byte[8290];
private readonly static HttpListener listener = new HttpListener();
public NotifyCallbackHelper(string callBackUrl)
{
this.callBackUrl = callBackUrl;
}
public void StartNotify()
{
listener.Prefixes.Add(callBackUrl);
listener.Start();
listener.BeginGetContext(NotifyCallback,listener);
}
public void NotifyCallback(IAsyncResult result)
{
try
{
HttpListener listener =(HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
ThreadPool.QueueUserWorkItem(new WaitCallback(ReadWrite),context);
listener.BeginGetContext(NotifyCallback, listener);
}
catch(Exception ex)
{
}
}
public void ReadWrite(object o)
{
HttpListenerContext context = o as HttpListenerContext;
Stream s = context.Request.InputStream;
s.Read(buffer, 0, buffer.Length);
context.Response.StatusCode = 200;
}
public void Close()
{
listener.Close();
}
}
1.先声明一个你需要的委托!在根据这个委托声明一个事件!
2.在你需要的地方调用那个事件,并把参数传递给事件。(注意先判断是否为空!因为事件可能没被注册!)
3.外部注册这个事件!就和.net事件一样的用法
不知道是否满足你的欲望