using System;
|
using System.Linq;
|
using System.Web;
|
using HDL_ON.UI;
|
|
namespace HDL_ON.DAL.Net
|
{
|
public static class HttpListener
|
{
|
private static System.Net.HttpListener listener;
|
|
/// <summary>
|
/// 启动监听及处理
|
/// </summary>
|
public static void Start(System.Net.IPAddress ipAddress, int port)
|
{
|
try
|
{
|
if (listener == null)
|
{
|
listener = new System.Net.HttpListener();
|
|
listener.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous;
|
|
//listener.Prefixes.Add (string.Format ("http://{0}:{1}/", ipAddress.ToString (), port));
|
listener.Prefixes.Add($"http://+:{port}/");
|
listener.Start();
|
|
//beginGetContext(listener);
|
contextThread = new System.Threading.Thread(() =>
|
{
|
while (true)
|
{
|
var context = listener.GetContext();
|
System.Threading.Tasks.Task.Run(() =>
|
{
|
manager(context);
|
});
|
}
|
})
|
{ IsBackground = true };
|
contextThread.Start();
|
MainPage.Log("HttpListener已经启动!");
|
}
|
}
|
catch (Exception e)
|
{
|
MainPage.Log("HttpListener启动失败!\r\n" + e.Message);
|
}
|
}
|
/// <summary>
|
///
|
/// </summary>
|
static System.Threading.Thread contextThread;
|
/// <summary>
|
/// 请求事件
|
/// </summary>
|
/// <param name="commandType">请求命令</param>
|
/// <param name="outStream">输出流</param>
|
/// <param name="inStream">输入流</param>
|
public delegate void RequestEventHandler(System.Collections.Specialized.NameValueCollection nameValueCollection, System.IO.Stream outputStream, System.IO.Stream inputStream);
|
|
/// <summary>
|
/// 请求事件
|
/// </summary>
|
public static event RequestEventHandler EventHandler;
|
|
/// <summary>
|
/// 开始异步接收http请求
|
/// </summary>
|
/// <param name="listerner"></param>
|
private static void beginGetContext(System.Net.HttpListener listerner)
|
{
|
try
|
{
|
if (listerner == null || !listerner.IsListening)
|
{
|
return;
|
}
|
listerner.BeginGetContext(new System.AsyncCallback(getContextCallBack), null);
|
|
}
|
catch
|
{
|
System.Threading.Thread.Sleep(1);
|
beginGetContext(listerner);
|
}
|
}
|
|
/// <summary>
|
/// 获取http请求,并处理
|
/// </summary>
|
/// <param name="ar"></param>
|
private static void getContextCallBack(System.IAsyncResult ar)
|
{
|
beginGetContext(listener);
|
try
|
{
|
System.Net.HttpListenerContext context = listener.EndGetContext(ar);
|
manager(context);
|
}
|
catch { }
|
}
|
|
/// <summary>
|
/// 关闭
|
/// </summary>
|
public static void Close()
|
{
|
try
|
{
|
if (contextThread != null)
|
{
|
contextThread.Abort();
|
contextThread = null;
|
}
|
}
|
catch { }
|
try
|
{
|
if (listener != null)
|
{
|
listener.Close();
|
listener = null;
|
}
|
}
|
catch { }
|
MainPage.Log("HttpListener已经关闭!");
|
}
|
|
private static void manager(System.Net.HttpListenerContext httpListenerContext)
|
{
|
System.Net.HttpListenerResponse httpListenerResponse = null;
|
|
try
|
{
|
#region
|
httpListenerResponse = httpListenerContext.Response;
|
|
httpListenerResponse.StatusCode = 200;//设置返回给客服端http状态代码
|
httpListenerResponse.ContentEncoding = System.Text.Encoding.UTF8;
|
|
|
if (EventHandler != null)
|
{
|
EventHandler(HttpUtility.ParseQueryString(System.IO.Path.GetFileName(httpListenerContext.Request.RawUrl)), httpListenerResponse.OutputStream, httpListenerContext.Request.InputStream);
|
}
|
httpListenerResponse.OutputStream.Flush();
|
|
#endregion
|
}
|
catch
|
{
|
|
}
|
finally
|
{
|
#region
|
try
|
{
|
httpListenerContext.Request.InputStream.Close();
|
}
|
catch { }
|
|
try
|
{
|
httpListenerResponse.OutputStream.Close();
|
}
|
catch { }
|
|
|
try
|
{
|
httpListenerResponse.Close();
|
}
|
catch { }
|
|
#endregion
|
}
|
}
|
|
public static void PostData(string url, string postJson)
|
{
|
System.IO.Stream outputStream = null;
|
System.IO.Stream inputStream = null;
|
try
|
{
|
System.Net.HttpWebRequest webReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(url));
|
webReq.Method = "POST";
|
//webReq
|
|
byte[] byteData = System.Text.Encoding.UTF8.GetBytes(postJson);
|
//定义传送数据格式
|
webReq.ContentType = "application/json";
|
webReq.Accept = "application/json";
|
//webReq.ContentType = "application/x-www-form-urlencoded";
|
//webReq.Timeout = 1000*2;
|
webReq.ContentLength = byteData.Length;
|
//定义Stream信息
|
outputStream = webReq.GetRequestStream();
|
outputStream.Write(byteData, 0, byteData.Length);
|
outputStream.Flush();
|
outputStream.Close();
|
|
//inputStream= webReq.GetResponse().GetResponseStream();
|
}
|
catch
|
{
|
throw new Exception("发送失败");
|
}
|
finally
|
{
|
if (null != outputStream)
|
{
|
outputStream.Close();
|
}
|
if (null != inputStream)
|
{
|
inputStream.Close();
|
}
|
}
|
}
|
}
|
}
|