如何在 C# HTTPListener 中使用 CSS 和 JS 请求完整的 HTML 页面
How to request full HTML page with CSS and JS in C# HTTPListener
我想编写一个 c# Http 网络服务器。如果请求 URL,我想将带有 CSS 和 JS 的 HTML 页面发送给客户端。我该怎么做?
static void Main(string[] args)
{
HttpListener server = new HttpListener(); // this is the http server
//server.Prefixes.Add("http://127.0.0.1/"); //we set a listening address here (localhost)
server.Prefixes.Add("http://localhost:2002/");
server.Start(); // and start the server
Console.WriteLine("Server started...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
byte[] buffer = Encoding.UTF8.GetBytes("<html></html>");
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
我想把完整的HTMLCSSJS网站发给客户
抱歉我的英语不好。
如果您提到的客户端能够使用和评估 JS 和 CSS,那么您可以将 javascript/CSS 作为内联文件或作为单独文件作为响应的一部分包含在内,如下所示。 .
byte[] buffer = Encoding.UTF8.GetBytes("<html>
<head>
<title>Page title</title>
<link rel='stylesheet' type='text/css' href='styles.css'>
</head>
<body>
</body>
<script src='js/script.js'></script>
</html>");
正如我刚刚发现的那样,我现在可以解决我的问题了。
我在 HTTPListenerContext class.
中尝试了 Request URL / RawURL 内容
static void Main(string[] args)
{
HttpListener server = new HttpListener(); // this is the http server
//server.Prefixes.Add("http://127.0.0.1/"); //we set a listening address here (localhost)
server.Prefixes.Add("http://localhost:2002/");
server.Start(); // and start the server
Console.WriteLine("Server started...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
Console.WriteLine("URL: {0}", context.Request.Url.OriginalString);
Console.WriteLine("Raw URL: {0}", context.Request.RawUrl);
byte[] buffer = File.ReadAllBytes("." + context.Request.RawUrl.Replace("%20", " "));
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
我只发送客户端(用户)请求的文件。感谢我的好 HTML 网站,它自动请求图片和 CSS 和 JS 文件。因此,当我进入 http://localhost:2002/ 时,它会自动向我发送完整的网站。
抱歉我的英语不好
我想编写一个 c# Http 网络服务器。如果请求 URL,我想将带有 CSS 和 JS 的 HTML 页面发送给客户端。我该怎么做?
static void Main(string[] args)
{
HttpListener server = new HttpListener(); // this is the http server
//server.Prefixes.Add("http://127.0.0.1/"); //we set a listening address here (localhost)
server.Prefixes.Add("http://localhost:2002/");
server.Start(); // and start the server
Console.WriteLine("Server started...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
byte[] buffer = Encoding.UTF8.GetBytes("<html></html>");
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
我想把完整的HTMLCSSJS网站发给客户
抱歉我的英语不好。
如果您提到的客户端能够使用和评估 JS 和 CSS,那么您可以将 javascript/CSS 作为内联文件或作为单独文件作为响应的一部分包含在内,如下所示。 .
byte[] buffer = Encoding.UTF8.GetBytes("<html>
<head>
<title>Page title</title>
<link rel='stylesheet' type='text/css' href='styles.css'>
</head>
<body>
</body>
<script src='js/script.js'></script>
</html>");
正如我刚刚发现的那样,我现在可以解决我的问题了。
我在 HTTPListenerContext class.
中尝试了 Request URL / RawURL 内容static void Main(string[] args)
{
HttpListener server = new HttpListener(); // this is the http server
//server.Prefixes.Add("http://127.0.0.1/"); //we set a listening address here (localhost)
server.Prefixes.Add("http://localhost:2002/");
server.Start(); // and start the server
Console.WriteLine("Server started...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
Console.WriteLine("URL: {0}", context.Request.Url.OriginalString);
Console.WriteLine("Raw URL: {0}", context.Request.RawUrl);
byte[] buffer = File.ReadAllBytes("." + context.Request.RawUrl.Replace("%20", " "));
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
我只发送客户端(用户)请求的文件。感谢我的好 HTML 网站,它自动请求图片和 CSS 和 JS 文件。因此,当我进入 http://localhost:2002/ 时,它会自动向我发送完整的网站。
抱歉我的英语不好