如何在我创建的服务器中运行html文件?
问题描述:
我刚刚使用以下代码创建了一个服务器,它只是打印消息.现在我需要在该服务器上运行一个html页面.如何做到这一点?我的代码如下所示.
Hi,
I just created a server using the following code.It just printing the message.Now i need to run a html page in this server.How to do that?My codes are shown below.
static void Main(string[] args)
{
HttpListener listner = null;
try
{
listner= new HttpListener();
listner.Prefixes.Add("http://localhost:1300/simpleserver/");
listner.Start();
while(true)
{
Console.WriteLine("Waiting....");
HttpListenerContext context =listner.GetContext();
string msg = "Welcome to the server";
context.Response.ContentLength64=Encoding.UTF8.GetByteCount(msg);
context.Response.StatusCode=(int)HttpStatusCode.OK;
using(Stream stream=context.Response.OutputStream)
{
using(StreamWriter writer =new StreamWriter(stream))
{
writer.Write(msg);
}
}
Console.WriteLine("Message Sent.....");
}
}
catch(WebException e)
{
Console.WriteLine(e.Status);
}
}
谢谢
Thanks
答
HTML文件无法在服务器中看到/呈现,它们是在Web浏览器或类似的读取和解释HTML的应用程序中呈现的.您可以选择一组免费的主要浏览器. :-)
您知道,您的代码确实与某个服务器相关,我可以通过HttpListener
看到它,但是由于您的问题使您怀疑您了解服务器和客户端的情况,因此我很难相信您确实创建了HTTP服务器… :-)—SA
HTML files are not seen/rendered in server, they are rendered in a Web browser or similar application reading and interpreting HTML. You can choose form a set of major browsers available free of charge. :-)
You know, your code really relevant to some server, I can see it byHttpListener
, but as your question creates a doubt that you understand what happens of server and client sides, I hardly can believe that you really created an HTTP server… :-)—SA
您必须解析传入的请求并通过发送关联文件的html内容进行响应.
看一下以下链接:
一个简单的HTTP服务器
You have to parse the incoming request and respond by sending the html contents of the associated file.
Take a look at the following links :
A Simple HTTP Server