从Windows服务侦听HTTP POST消息

时间:2016-04-05 16:35:57

标签: c# httprequest

我有一个监听HTTP请求的类(作为Window Service安装)。

using System;
using System.ServiceProcess;
using System.IO;
using System.Net;
using System.Net.Sockets;

class Program : System.ServiceProcess.ServiceBase
{
    public static String navn = "PM_RequestHandler";
    private bool RunThread = true;

    public void StartMe()
    {
        IPAddress localAddr = System.Net.IPAddress.Parse("127.0.0.1");
        System.Net.Sockets.TcpListener server = new System.Net.Sockets.TcpListener(localAddr, 1234); // http://localhost:1234
        server.Start();

        Byte[] bytes = new Byte[1024];
        String data = null;

        while (RunThread)
        {
            TcpClient client =  server.AcceptTcpClient();
            data = null;

            NetworkStream stream = client.GetStream(); 
            stream.Read(bytes, 0, bytes.Length);

            data = System.Text.Encoding.ASCII.GetString(bytes);

            // LOG
            StreamWriter sw = new StreamWriter("c:\\PM_RequestHandler.txt", true);
            sw.WriteLine(data);
            sw.Close();

            client.Close();
        }
    }

    protected override void OnStart(string[] args)
    {
        System.Threading.Thread thr = new System.Threading.Thread(new
        System.Threading.ThreadStart(this.StartMe));
        thr.Start();
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        RunThread = false;
        base.OnStop();
    }

    static void Main(string[] args)
    {
        System.ServiceProcess.ServiceBase.Run(new Program());
    }
}

我有一个我(本地)运行的Web服务来尝试我以前的课程。 我使用SoapUI来调用它。 问题是Web服务使用POST请求而我无法捕获它们。

我的日志为空,似乎该类只捕获在浏览器中手动生成的Get个请求。 为什么会这样?

0 个答案:

没有答案