我希望在C#控制台应用程序中解析HTTP POST请求并需要一些帮助。该应用程序使用Owin运行“Web服务器”。有关应用程序的详细信息,请单击此处,有关相关代码的当前“稳定版本”,请单击此处。
我正在扩展上述应用程序以通过Web界面启用配置。例如,该应用程序目前报告大量参数。我希望最终用户能够选择在网络上报告哪些参数。为此,我对上述代码进行了一些修改:
我正在扩展上述应用程序以通过Web界面启用配置。例如,该应用程序目前报告大量参数。我希望最终用户能够选择在网络上报告哪些参数。为此,我对上述代码进行了一些修改:
using Microsoft.Owin;
using Owin;
.........
[assembly: OwinStartup(typeof(SensorMonHTTP.WebIntf))]
.........
.........
namespace SensorMonHTTP
{
...
public class WebIntf
{
public void Configuration(IAppBuilder app)
{
app.Run(context =>
{
var ConfigPath = new Microsoft.Owin.PathString("/config");
var ConfigApplyPath = new Microsoft.Owin.PathString("/apply_config");
var SensorPath = new Microsoft.Owin.PathString("/");
if (context.Request.Path == SensorPath)
{
return context.Response.WriteAsync(GetSensorInfo());
/* Returns JSON string with sensor information */
}
else if (context.Request.Path == ConfigPath)
{
/* Generate HTML dynamically to list out available sensor
information with checkboxes using Dynatree: Tree3 under
'Checkbox & Select' along with code to do POST under
'Embed in forms' in
http://wwwendt.de/tech/dynatree/doc/samples.html */
/* Final segment of generated HTML is as below:
<script>
.....
$("form").submit(function() {
var formData = $(this).serializeArray();
var tree = $("#tree3").dynatree("getTree");
formData = formData.concat(tree.serializeArray());
// alert("POST this:\n" + jQuery.param(formData));
// -- This gave the expected string in an alert when testing out
$.post("apply_config", formData);
return true ;
});
......
</script></head>
<body>
<form action="apply_config" method="POST">
<input type="submit" value="Log Selected Parameters">
<div id="tree3" name="selNodes"></div>
</body>
</html>
End of generated HTML code */
}
else if (context.Request.Path == ConfigApplyPath)
{
/* I want to access and parse the POST data here */
/* Tried looking into context.Request.Body as a MemoryStream,
but not getting any data in it. */
}
}
}
........
}
有人能帮我解决如何在上述代码结构中访问POST数据的问题吗?
提前致谢!