1、创建WindowsService,如下代码
using System;
using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
public static int TerminalPort = 12800;
public static Thread ListeningThread;
public static int Port;
public static Encoding Encoding;
protected override void OnStart(string[] args)
{
InitialAcceptor(TerminalPort);
}
protected override void OnStop()
{
}
public void DebugTest()
{
Console.WriteLine("StartService...");
InitialAcceptor(TerminalPort);
Console.WriteLine("Terminal Listened on Port=" + TerminalPort);
}
public void InitialAcceptor(int tunnelPort)
{
Port = tunnelPort;
Encoding = Encoding.UTF8;
ListeningThread = new Thread(new ThreadStart(StartListening));
ListeningThread.IsBackground = Environment.OSVersion.Platform != PlatformID.Unix;//
ListeningThread.Start();
}
#region MyRegion
//internal void StartListening()
//{
// try
// {
// while (true)
// {
// try
// {
// listener = new HttpListener();
// listener.Prefixes.Add("http://+:" + Port + "/");
// listener.Start();
// }
// catch
// {
// Thread.Sleep(1000);
// }
// try
// {
// while (true)
// {
// IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
// result.AsyncWaitHandle.WaitOne();
// }
// }
// catch (Exception e)
// {
// }
// }
// }
// catch
// {
// }
//}
#endregion
internal void StartListening()
{
try
{
while (true)
{
try
{
#region MyRegion
//服务端:
//var listener = new HttpListener();
//listener.Prefixes.Add("http://*:8080/");
//listener.Start();
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:" + 1234 + "/");
listener.Start();
while (true)
{
IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
result.AsyncWaitHandle.WaitOne();
}
#endregion
}
catch
{
Thread.Sleep(1000);
}
}
}
catch
{
}
}
private void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
processRequest(context);
}
private void processRequest(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
try
{
//string url = System.Web.HttpUtility.UrlDecode(request.RawUrl);
string ss = request.QueryString["eid"];
context.Response.ContentEncoding = UTF8Encoding.UTF8;
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
//context.Response.ContentType = "image/jpg";
//context.Response.ContentLength64 = buffer.Length;
var output = context.Response.OutputStream;
byte[] nn = System.Text.Encoding.Default.GetBytes("qqqqqqqqqqqq");
output.Write(nn, 0, nn.Length);
output.Close();
}
catch (Exception e)
{
}
finally
{
}
}
}
}
2、创建控制台,测试启动WindowsService服务
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using WindowsService;
namespace WindowsService1TestConsole
{
class Program
{
static void Main(string[] args)
{
try
{
Service1 service1 = new Service1();
#if DEBUG
service1.DebugTest();
#endif
Thread.CurrentThread.IsBackground = false;
Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Source);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.InnerException != null ? e.InnerException.ToString() : "");
Console.ReadLine();
}
}
}
}
3、测试http请求
直接 地址栏输入:http://127.0.0.1:1234/?eid=30
结果:qqqqqqqqqqqq
写测试代码
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();//返回结果
myStreamReader.Close();
myResponseStream.Close();
龙腾一族至尊龙骑