我已经在 c# 中使用以下代码创建本地主机
static HttpListener _httpListener = new HttpListener();
[STAThread]
static void Main()
{
Console.WriteLine("Starting server...");
_httpListener.Prefixes.Add("http://localhost:5000/"); // add prefix "http://localhost:5000/"
_httpListener.Start(); // start server (Run application as Administrator!)
Console.WriteLine("Server started.");
Thread _responseThread = new Thread(ResponseThread);
_responseThread.Start(); // start the response thread
}
我的问题是我在文件夹中有一个 c# wesite。我想使用创建的 localhost:5000 来运行这个网站。 谁能建议我适当的解决方案。
谢谢 尼玛
static void ResponseThread()
{
while (true)
{
HttpListenerContext context = _httpListener.GetContext();
byte[] _responseArray = Encoding.UTF8.GetBytes( "<html><head><title>Localhost server -- port 5000</title></head>" +
<body>Welcome to <strong>Localhost server</strong> -- <em>port 5000!</em></body></html>");
byte[] _responseArray = Encoding.UTF8.GetBytes(str); // get the bytes to response
context.Response.OutputStream.Write(_responseArray, 0, _responseArray.Length); // write bytes to the output stream
context.Response.KeepAlive = false; // set the KeepAlive bool to false
context.Response.Close(); // close the connection
Console.WriteLine("Respone given to a request.");
}
}
消息显示在 localhost:500 中,但我想使用此代码运行我的 c# 网站。