C# 正在侦听来自windows服务的HTTP POST消息
我有一个类(作为窗口服务安装)侦听HTTP请求C# 正在侦听来自windows服务的HTTP POST消息,c#,httprequest,C#,Httprequest,我有一个类(作为窗口服务安装)侦听HTTP请求 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
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
请求。
为什么会这样?如果要调试代码,可以将其粘贴到控制台应用程序或附加到正在运行的服务进程中。POST和GET没有区别,它们只是TCP连接,在http请求中是“POST”而不是“GET”(是的,我知道,它非常简单),所以问题应该在其他地方。您是否尝试过从浏览器直接向侦听器发送POST请求(使用类似的方式)?您不想尝试实现自己的HTTP服务器(这似乎是您在这里尝试的最基本的方式)。如果您在Windows上运行,我强烈建议您考虑使用HttpListener或其他预构建的HTTP协议处理程序。@CodingGorilla HttpListener在使用mono的Linux/MacOS上也非常有效;)我现在将尝试与邮递员铬。我会让你知道的。好的,它起作用了。问题是安装的服务,有时会无缘无故地停止。