Не удалось получить данные публикации из HttpListenerRequest

Кажется, я не могу получить какие-либо данные сообщения в следующем простом приложении веб-сервера. request.InputStream никогда ничего не возвращает.

HTML из формы публикации находится в коде. Это очень просто — просто кнопка и кнопка отправки.

Я что-то упускаю? Я раньше не использовал сборку HttpListener, поэтому не знаю, упускаю ли я что-то простое. Есть ли другая сборка, которую я должен использовать.

Любая помощь будет очень высоко ценится!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        WebServer ws = new WebServer(SendResponse, "http://localhost:8088/");
        ws.Run();
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
        ws.Stop();
    }

    public static string SendResponse(HttpListenerRequest request)
    {
        try
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(request.InputStream, request.ContentEncoding))
            {
                string s = reader.ReadToEnd();
                Console.WriteLine("InputStream: {0}", s);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}", ex.Message);
        }

        return @"<html><body><form action='http://localhost:8088/' method='post'><input type='text' value='My Input'><input type='submit'></form></body></html>";
    }
}

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

    public WebServer(string[] prefixes, Func<HttpListenerRequest, string> 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) { }

    public void Run()
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            Console.WriteLine("Listening...");
            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 {}
                        finally
                        {
                            ctx.Response.OutputStream.Close();
                        }
                    }, _listener.GetContext());
                }
            }
            catch {}
        });
    }

    public void Stop()
    {
        _listener.Stop();
        _listener.Close();
    }
}

person Dave    schedule 29.03.2013    source источник


Ответы (1)


DOH — мне нужен атрибут имени во входном теге, чтобы он отображался в данных поста.

Больше 10 часов я никогда не верну!

person Dave    schedule 01.04.2013