none
HttpListener搭建的小服务器 RRS feed

  • 问题

  • 我用HttpListener搭建的WEB服务器,但奇怪的是它只能接受后缀名为aspx的文件请求,比如说test.aspx,他能返回正确的结果,我请求test.htm文件,她不返回任何内容,有人遇过类似的问题吗?
    2009年8月27日 8:32

答案

  • 您好,我把代码贴出来,您参考一下:
    class HttpListenerDemo
        {
            // This example requires the System and System.Net namespaces.
            public static void SimpleListenerExample(string[] prefixes)
            {
                if (!HttpListener.IsSupported)
                {
                    Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                    return;
                }
                // URI prefixes are required,
                // for example "http://contoso.com:8080/index/".
                if (prefixes == null || prefixes.Length == 0)
                    throw new ArgumentException("prefixes");

                // Create a listener.
                HttpListener listener = new HttpListener();
                // Add the prefixes.
                foreach (string s in prefixes)
                {
                    listener.Prefixes.Add(s);
                }
                listener.Start();
                Console.WriteLine("Listening...");
                string m_end = "";
                // Note: The GetContext method blocks while waiting for a request.
                while (m_end != "end")
                {
                    HttpListenerContext context = listener.GetContext();
                    HttpListenerRequest request = context.Request;
                    string url = request.Url.ToString();
                    // Obtain a response object.
                    HttpListenerResponse response = context.Response;
                    // Construct a response.
                    string responseString = "<HTML><BODY> Hello world!<br>" + url + "</BODY></HTML>";
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                    // Get a response stream and write the response to it.
                    response.ContentLength64 = buffer.Length;
                    System.IO.Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    // You must close the output stream.
                    output.Close();
                   
                    if (url.IndexOf("end") != -1)
                    {
                        m_end = "end";
                    }
                }
                listener.Stop();
            }

            public static void Main()
            {
                string[] m_Prefixs = new string[1];
                m_Prefixs[0] = "http://localhost:8080/customerData/";
                HttpListenerDemo.SimpleListenerExample(m_Prefixs);
            }
        }

    2009年8月31日 1:40

全部回复

  • 您好,个人的观点请参考:
    因为web server直接返回htm请求的内容,并不需要通过IIS上的应用程序扩展来解析。
    尝试在IIS的网站主目录的配置中加入htm的支持,处理的可执行文件与aspx的相同。
    以上未验证过,如果有结果请告知,谢谢!
    2009年8月27日 13:41
  • 我用的不是IIS,而是用HttpListener搭建的WEB服务器,如果是IIS的话不会有这个问题的。
    2009年8月28日 0:42
  • 您好,我用msdn上的一个例子调试了一下,发现只要是前缀一样。任意扩展名结尾的文件都可以。
    htm也可以。是不是您的代码中做了扩展名判断处理。
    2009年8月28日 2:43
  • 没有啊,我也用的是MSDN上的小例子,就几十行代码,没有什么特殊处理。

    2009年8月31日 1:17
  • 您好,我把代码贴出来,您参考一下:
    class HttpListenerDemo
        {
            // This example requires the System and System.Net namespaces.
            public static void SimpleListenerExample(string[] prefixes)
            {
                if (!HttpListener.IsSupported)
                {
                    Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                    return;
                }
                // URI prefixes are required,
                // for example "http://contoso.com:8080/index/".
                if (prefixes == null || prefixes.Length == 0)
                    throw new ArgumentException("prefixes");

                // Create a listener.
                HttpListener listener = new HttpListener();
                // Add the prefixes.
                foreach (string s in prefixes)
                {
                    listener.Prefixes.Add(s);
                }
                listener.Start();
                Console.WriteLine("Listening...");
                string m_end = "";
                // Note: The GetContext method blocks while waiting for a request.
                while (m_end != "end")
                {
                    HttpListenerContext context = listener.GetContext();
                    HttpListenerRequest request = context.Request;
                    string url = request.Url.ToString();
                    // Obtain a response object.
                    HttpListenerResponse response = context.Response;
                    // Construct a response.
                    string responseString = "<HTML><BODY> Hello world!<br>" + url + "</BODY></HTML>";
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                    // Get a response stream and write the response to it.
                    response.ContentLength64 = buffer.Length;
                    System.IO.Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    // You must close the output stream.
                    output.Close();
                   
                    if (url.IndexOf("end") != -1)
                    {
                        m_end = "end";
                    }
                }
                listener.Stop();
            }

            public static void Main()
            {
                string[] m_Prefixs = new string[1];
                m_Prefixs[0] = "http://localhost:8080/customerData/";
                HttpListenerDemo.SimpleListenerExample(m_Prefixs);
            }
        }

    2009年8月31日 1:40