【问题标题】:How to serve relative files from HttpListener如何从 HttpListener 提供相关文件
【发布时间】:2014-04-10 01:11:44
【问题描述】:

我正在学习HttpListener。我正在使用HttpListener 创建一个小型应用程序,它是一个网络服务器(如下 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)。注意,没有 ASP.NET 的东西。

在从_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>
   ...

但是,正如预期的那样,.css.js 文件似乎没有被提供(我可以说是因为在提供 html 后客户端上没有预期的样式或行为)。

我该如何提供这些文件,我需要对HttpServerUtility.MapPath 做些什么吗?如果是这样,你能指出一些例子吗?

或者我是否需要扫描我将要提供的 HTML 并打开这些文件(递归地)读取并返回这些文件?我希望不会。

顺便说一句,提供此服务的 C# 代码如下,其中我的 _responderMethod 只是返回 HTML 文件的字符串,如上所述:

我初始化并启动服务器如下:

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

类构造函数差不多:

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) { }

.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
    });
}

我的SendResponse()

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

【问题讨论】:

  • 我编辑了你的问题。与普通论坛网站不同,我们不需要问题标题中的标签或问题中的 hiThanks 等称呼。
  • 你能把代码放在你的问题中,显示听众的方法吗?它将是带有 HttpListener.Start 方法调用的那个。
  • 客户端将向您的服务器请求这些文件。您是否看到来自客户端(我假设它是浏览器)对 .css 和 .js 文件的请求?如果是这样,你在为他们服务吗?提供 HTML 时,您将什么设置为响应内容类型?
  • 嗨,亚当,吉姆,非常感谢您的帮助。我现在用更多代码更新了我的帖子。 @JimMischel 客户端是一个浏览器,让我检查一下我是否收到了这些文件的请求...我认为我没有收到这些请求...
  • 实际上,我在 request.m_RawUrl 中获取了请求的文件!我不知道为什么我在发布之前没有检查。现在是为他们服务的问题。与 File.ReadAllText(如果是文本)和 File.ReadAllBytes(如果像 PNG 文件这样的二进制文件)一样,首选的方法是什么?

标签: c# html httplistener


【解决方案1】:

如果您的 HTML 指定站点上的脚本或 CSS 文件,客户端浏览器将请求它们。您的服务器也必须提供这些文件。脚本和 CSS 文件是您提供的文本文件,其提供方式与提供 HTML 文件的方式非常相似,但您需要确保相应地更改内容类型标头。您以二进制格式提供二进制文件(图像等)。同样,您需要确保将内容类型设置为表明它是图像。

【讨论】:

  • 嗨,吉姆,谢谢,我搞定了。正如您所提到的,我需要返回内容类型并将数据作为 byte[] 从 File.ReadAllBytes 返回,谢谢!
猜你喜欢
  • 2020-05-05
  • 2012-11-03
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2012-12-08
  • 2018-06-06
  • 2015-01-18
相关资源
最近更新 更多