我在C#/ Mono中编写了一个在Raspberry Pi上运行的HTTP侦听器应用程序。如果我从我的PC发送一个带有Postman的HTTP请求,请求不会到达Raspberry Pi,或者监听器没有读取它。换句话说,ShowRequestData()中的断点永远不会被击中。
但是,如果我在我的电脑上运行应用程序(使用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上进行配置吗?
答案 0 :(得分:0)
我发现了问题。看来我必须写出Raspberry Pi的确切IP:
httpListener.Prefixes.Add("http://" + Helpers.GetLocalIP() + ":8080/");