简体   繁体   English

如何从HttpListener提供相关文件

[英]How to serve relative files from HttpListener

I am learning the HttpListener . 我正在学习HttpListener I am creating a small app that is a webserver by using the HttpListener (following http://msdn.microsoft.com/en-us/library/system.net.httplistener%28v=vs.110%29.aspx and https://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx ). 我正在使用HttpListener创建一个小型应用程序,这是一个Web服务器(请访问http://msdn.microsoft.com/en-us/library/system.net.httplistener%28v=vs.110%29.aspxhttps: //www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx )。 Note, no ASP.NET stuff. 注意,没有ASP.NET的东西。

In my function that is called from _responderMethod , I basically return HTML (that is read from a physical file on disk) which contains something like: 在我从_responderMethod调用的函数中,我基本上返回HTML(从磁盘上的物理文件中读取),其中包含以下内容:

   ...
   <link href="css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
   <script src="js/jquery-1.10.2.js"></script>
   <script src="js/jquery-ui-1.10.4.custom.js"></script>
   ...

However, as to be expected, the .css and .js files don't seem to be served (I can tell because there's no styles or behaviors as expected on the client after the html has been served). 但是,正如预期的那样,似乎没有提供.css.js文件(我可以说,因为在提供html之后,客户端上没有预期的样式或行为)。

How do I go about serving this files, do I need to do something with HttpServerUtility.MapPath ? 我如何提供这些文件,我需要用HttpServerUtility.MapPath做些什么吗? if so, could you point me to some examples on that? 如果是的话,你能指点我的一些例子吗?

Or do I need to scan the HTML I am about to serve and open those files up (recursively) read and return those? 或者我是否需要扫描我即将提供的HTML并打开这些文件(递归地)读取并返回这些文件? I hope not. 我希望不是。

BTW, the C# code that serves this is as follows, where my _responderMethod just returns the string of the HTML file as it is stated above: BTW,服务于此的C#代码如下,其中我的_responderMethod只返回HTML文件的字符串,如上所述:

I initialize and start the server as follows: 我按如下方式初始化并启动服务器:

WebServer ws = new WebServer(program.SendResponse, "http://<myip>:8080/");
ws.Run();

the class constructor is pretty much: 类构造函数非常多:

public class WebServer
{
    private readonly HttpListener _listener = new HttpListener();
    private readonly Func<HttpListenerRequest, string> _responderMethod;

    public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
    { 
        // A responder method is required
        if (method == null)
            throw new ArgumentException("method");

        foreach (string s in prefixes)
            _listener.Prefixes.Add(s);

         _responderMethod = method;
         _listener.Start();
}
public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
    : this(prefixes, method) { }

the .Run() is: .Run()是:

public void Run()
{
    ThreadPool.QueueUserWorkItem((o) =>
    {
        Console.WriteLine("Webserver running...");
        try
        {
            while (_listener.IsListening)
            {
                ThreadPool.QueueUserWorkItem((c) =>
                {
                    var ctx = c as HttpListenerContext;
                    try
                    {
                        string rstr = _responderMethod(ctx.Request);
                        byte[] buf = Encoding.UTF8.GetBytes(rstr);
                        ctx.Response.ContentLength64 = buf.Length;
                        ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                    }
                    catch { } // suppress any exceptions
                    finally
                    {
                        // always close the stream
                        ctx.Response.OutputStream.Close();
                    }
                }, _listener.GetContext());
            }
        }
        catch { } // suppress any exceptions
    });
}

My SendResponse() : 我的SendResponse()

public string SendResponse(HttpListenerRequest request)
{
      return File.ReadAllText(@"static\index.html");
}

If your HTML specifies script or CSS files that are on your site, the client browser is going to request them. 如果您的HTML指定您站点上的脚本或CSS文件,则客户端浏览器将请求它们。 Your server has to serve up those files, too. 您的服务器也必须提供这些文件。 Scripts and CSS files are text files that you serve in much the same way that you serve HTML files, although you'll want to be sure to change the content type header accordingly. 脚本和CSS文件是您提供的文本文件,与提供HTML文件的方式非常相似,但您需要确保相应地更改内容类型标题。 You serve binary files (images, etc.) in binary format. 您以二进制格式提供二进制文件(图像等)。 Again, you'll want to be sure to set the content type to indicate that it's an image. 同样,您需要确保设置内容类型以指示它是图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM