Http Server的一个示例

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;
using System.Web;
using System.Collections.Specialized;


namespace GateWayInterface
{

public class Processor
{
public static FormMain mainForm;
private HttpListenerContext context;
public Processor(HttpListenerContext context)
{
this.context = context;
}
public void process()
{
string frommobile = "", servicenumber = "", content = "";
try
{
HttpListenerRequest request
= context.Request;
HttpListenerResponse response
= context.Response;
//安全检查
bool safeip = false;
IPEndPoint point
= request.RemoteEndPoint;
string[] ips = mainForm.Ini.ReadValue("safe", "ips").Split(',');
foreach (string ip in ips)
{
if (point.Address.ToString() == ip)
{
safeip
= true;
break;
}
}
if (!safeip)
{
Log.LogMo(
"HTTP Server", "非法调用:" + point.Address.ToString());
return;

}
//安全检查结束
System.Text.StringBuilder str = new System.Text.StringBuilder();
string query = request.Url.Query;
NameValueCollection nv
= HttpUtility.ParseQueryString(query, Encoding.UTF8);

foreach (string key in nv.AllKeys)
{
if (key == "fromobile") frommobile = nv[key];
if (key == "spcode") servicenumber = nv[key];
if (key == "content") content = nv[key];
}
//
if (frommobile != "" && content != "")
{
mainForm.DealUp(frommobile, servicenumber, content);
}

str.Append(
"");
string responseString = str.ToString();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64
= buffer.Length;
using (System.IO.Stream outputStream = response.OutputStream)
{
outputStream.Write(buffer,
0, buffer.Length);
outputStream.Close();
}
Log.LogMo(
"HTTP Server", "响应完毕" );

}
catch (Exception ex)
{
Log.LogMo(
"HTTP Server", "处理异常:" + ex.Message);
}
}

}

public class HttpServer
{
private HttpListener listener = new HttpListener();
private FormMain mainForm;
private IniFile ini;
public bool running=true;

public HttpServer(FormMain form)
{
this.mainForm = form;
this.ini = mainForm.Ini;
}
public void Listen()
{
if (!HttpListener.IsSupported)
{
return;
}
try
{
string port = ini.ReadValue("HttpServer", "port");
if (port == "") port = "8080";
listener.Prefixes.Add(
"http://+:" + port + "/");
listener.Start();
Log.LogMo(
"HTTP Server", "正在" + port + "端口监听");
Processor.mainForm
= mainForm;
while (running)
{
//获取一个客户端请求为止
HttpListenerContext context = listener.GetContext();
HttpListenerRequest req
= context.Request;
Log.LogMo(
"HTTP Server", "收到请求:" + req.RemoteEndPoint + " " + req.RawUrl);
Processor p
= new Processor(context);
Thread thread
= new Thread(new ThreadStart(p.process));
thread.Start();
}
}
catch(Exception ex)
{
Log.LogMo(
"监听线程", ex.Message);
}
//listener.Close();


}

}
}

在主程序中调用的方法:

        /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false);
try
{
ini
= new IniFile(AppDomain.CurrentDomain.BaseDirectory + "setting.ini");
if (ini == null) Log.LogMo("主线程\t", "没有获取配置文件");

}
catch (Exception ex)
{
Log.LogMo(
"主线程\t", "读取INI文件错误:"+ex.Message);
}

mainForm
= new FormMain(ini);
//启动HTTP服务器
httpServer = new HttpServer(mainForm);
mainForm.httpThread
= new Thread(new ThreadStart(httpServer.Listen));
mainForm.httpThread.IsBackground
= true;
mainForm.httpThread.Start();
Log.LogMo(
"主线程\t", "HTTP服务器启动。");

Application.Run(mainForm);
Log.LogMo(
"主线程\t", "程序运行中");
}


原文地址:https://www.cnblogs.com/Aricc/p/1340469.html