C# 无法从HttpListenerRequest获取post数据
在下面的简单web服务器应用程序中,我似乎无法检索任何post数据。request.InputStream从不返回任何内容 发帖表单中的HTML位于代码中。这是非常基本的-只是一个,和提交按钮 我错过什么了吗?我以前没有使用过HttpListener程序集,所以我不知道是否缺少一些简单的东西。我是否应该使用不同的程序集 任何帮助都将不胜感激C# 无法从HttpListenerRequest获取post数据,c#,html,postdata,httplistenerrequest,C#,Html,Postdata,Httplistenerrequest,在下面的简单web服务器应用程序中,我似乎无法检索任何post数据。request.InputStream从不返回任何内容 发帖表单中的HTML位于代码中。这是非常基本的-只是一个,和提交按钮 我错过什么了吗?我以前没有使用过HttpListener程序集,所以我不知道是否缺少一些简单的东西。我是否应该使用不同的程序集 任何帮助都将不胜感激 using System; using System.Collections.Generic; using System.Linq; using Syste
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();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
Net系统;
使用系统线程;
班级计划
{
静态void Main(字符串[]参数)
{
Web服务器ws=新的Web服务器(SendResponse,“http://localhost:8088/");
ws.Run();
控制台。WriteLine(“按任意键退出”);
Console.ReadKey();
ws.Stop();
}
公共静态字符串SendResponse(HttpListenerRequest请求)
{
尝试
{
使用(System.IO.StreamReader reader=new System.IO.StreamReader(request.InputStream,request.ContentEncoding))
{
字符串s=reader.ReadToEnd();
WriteLine(“InputStream:{0}”,s);
}
}
捕获(例外情况除外)
{
WriteLine(“错误:{0}”,例如Message);
}
返回@”;
}
}
公共类Web服务器
{
私有只读HttpListener _listener=new HttpListener();
私有只读函数响应方法;
公共Web服务器(字符串[]前缀,Func方法)
{
foreach(前缀中的字符串s)
_listener.Prefixes.Add(s);
_responderMethod=方法;
_listener.Start();
}
公共Web服务器(Func方法,params字符串[]前缀):this(前缀,方法){}
公开募捐
{
ThreadPool.QueueUserWorkItem((o)=>
{
Console.WriteLine(“监听…”);
尝试
{
while(_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c)=>
{
var ctx=c作为HttpListenerContext;
尝试
{
字符串rstr=\u responderMethod(ctx.Request);
字节[]buf=Encoding.UTF8.GetBytes(rstr);
ctx.Response.ContentLength64=基本长度;
ctx.Response.OutputStream.Write(buf,0,buf.Length);
}
捕获{}
最后
{
ctx.Response.OutputStream.Close();
}
},_listener.GetContext());
}
}
捕获{}
});
}
公共停车场()
{
_listener.Stop();
_listener.Close();
}
}
DOH-我需要输入标记中的name属性,以使其显示在post数据中
还有10多个小时我再也回不来了 DOH-我需要输入标记中的name属性,以使其显示在post数据中 还有10多个小时我再也回不来了