#region http服务监听器
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://192.168.10.21:3000/Temp/");
listener.Start();
Console.WriteLine("开启监听");
while (true)
{
HttpListenerContext context = listener.GetContext();
Console.WriteLine("来了一个请求");
new Thread((o) =>
{
HttpListenerContext con = (HttpListenerContext)o;
string IP = con.Request.Headers.GetValues("IP")[0].ToString();
string GUID = con.Request.Headers.GetValues("GUID")[0].ToString();
string THRID = con.Request.Headers.GetValues("THRID")[0].ToString();
Console.WriteLine(string.Format("客户端:IP:{0}, 线程:{1}, GUID:{2}", IP, THRID, GUID));
HttpListenerContext request = (HttpListenerContext)con;
request.Response.StatusCode = 200;
request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
request.Response.ContentType = "application/json";
con.Response.ContentEncoding = Encoding.UTF8;
byte[] buffer = Encoding.UTF8.GetBytes("我收到了" + DateTime.Now.ToString());
request.Response.ContentLength64 = buffer.Length;
var output = request.Response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
Console.WriteLine("处理完成");
}).Start(context);
}
#endregion
#region http请求 客户端
for (int i = 1; i <= 10; i++)
{
new Thread((x) =>
{
HttpHelper http = new HttpHelper();
HttpItem hi = new HttpItem();
hi.URL = "http://192.168.10.21:3000/Temp/";
hi.Method = "GET";
hi.Header.Add("IP:" + new Random().Next(100));
hi.Header.Add("GUID:" + Guid.NewGuid().ToString());
hi.Header.Add("THRID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("第" + x + "次请求");
string html = http.GetHtml(hi).Html;
Console.WriteLine("第" + x + "次请求返回信息" + html);
}).Start(i);
}
Console.ReadKey();