首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从HttpListenerRequest获取post数据

无法从HttpListenerRequest获取post数据
EN

Stack Overflow用户
提问于 2013-03-30 05:05:31
回答 1查看 1.5K关注 0票数 0

在下面这个简单的web服务器应用程序中,我似乎无法检索到任何post数据。request.InputStream从不返回任何内容。

发布表单中的HTML在代码中。这是非常基本的-只是一个,提交按钮。

我是不是遗漏了什么?我以前没有使用过HttpListener程序集,所以我不知道我是否遗漏了一些简单的东西。有没有我应该使用的不同程序集。

任何帮助都将不胜感激!

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-02 02:45:20

DOH -我在input标记中需要一个name属性才能让它显示在post数据中。

有10+小时我再也回不来了!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15711400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档