Mono 中的 HTTP 侦听器不读取 HTTP 请求
HTTP listener in Mono does not read HTTP request
我用 C#/Mono 编写了一个 HTTP 侦听器应用程序,运行s 在 Raspberry Pi 上。如果我从我的 PC 使用 Postman 发送 HTTP 请求,请求不会到达 Raspberry Pi 或者侦听器不会读取它。换句话说,永远不会命中 ShowRequestData() 中的断点。
但是,如果我 运行 我的 PC 上的应用程序(使用 Mono 或 NET 调试器),它可以工作。我的代码:
httpListenerWorker.DoWork += HttpListenerWorker_DoWork;
httpListener.Prefixes.Add("http://localhost:8080/");
try
{
httpListener.Start();
httpListenerWorker.RunWorkerAsync();
}
catch (Exception ex)
{
Console.WriteLine("Could not start HTTP listener");
}
private static void HttpListenerWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
httpContext = httpListener.GetContext();
httpRequestString = HTTP.ShowRequestData(httpContext.Request);
if (httpRequestString != "")
{
messageReceived = true;
threadWaitHandle.Set();
}
}
}
public static string ShowRequestData(HttpListenerRequest request)
{
if (!request.HasEntityBody)
{
return "";
}
Stream body = request.InputStream;
Encoding encoding = request.ContentEncoding;
StreamReader reader = new StreamReader(body, encoding);
string s = "";
s = reader.ReadToEnd();
body.Close();
reader.Close();
return s;
}
我试过使用 iptables 打开端口 8080,但没有成功:
iptables -A INPUT -p tcp --dport 8080 --jump ACCEPT
iptables -A OUTPUT -p tcp --dport 8080 --jump ACCEPT
iptables-save
是不是代码有问题?我需要在 Raspberry Pi?
上进行配置吗
我发现了问题。看来我必须写下 Raspberry Pi:
的确切 IP
httpListener.Prefixes.Add("http://" + Helpers.GetLocalIP() + ":8080/");
我用 C#/Mono 编写了一个 HTTP 侦听器应用程序,运行s 在 Raspberry Pi 上。如果我从我的 PC 使用 Postman 发送 HTTP 请求,请求不会到达 Raspberry Pi 或者侦听器不会读取它。换句话说,永远不会命中 ShowRequestData() 中的断点。
但是,如果我 运行 我的 PC 上的应用程序(使用 Mono 或 NET 调试器),它可以工作。我的代码:
httpListenerWorker.DoWork += HttpListenerWorker_DoWork;
httpListener.Prefixes.Add("http://localhost:8080/");
try
{
httpListener.Start();
httpListenerWorker.RunWorkerAsync();
}
catch (Exception ex)
{
Console.WriteLine("Could not start HTTP listener");
}
private static void HttpListenerWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
httpContext = httpListener.GetContext();
httpRequestString = HTTP.ShowRequestData(httpContext.Request);
if (httpRequestString != "")
{
messageReceived = true;
threadWaitHandle.Set();
}
}
}
public static string ShowRequestData(HttpListenerRequest request)
{
if (!request.HasEntityBody)
{
return "";
}
Stream body = request.InputStream;
Encoding encoding = request.ContentEncoding;
StreamReader reader = new StreamReader(body, encoding);
string s = "";
s = reader.ReadToEnd();
body.Close();
reader.Close();
return s;
}
我试过使用 iptables 打开端口 8080,但没有成功:
iptables -A INPUT -p tcp --dport 8080 --jump ACCEPT
iptables -A OUTPUT -p tcp --dport 8080 --jump ACCEPT
iptables-save
是不是代码有问题?我需要在 Raspberry Pi?
上进行配置吗我发现了问题。看来我必须写下 Raspberry Pi:
的确切 IPhttpListener.Prefixes.Add("http://" + Helpers.GetLocalIP() + ":8080/");