Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何从context.Request.InputStream获取POST表单值_C#_Http_Parsing_Post - Fatal编程技术网

C#如何从context.Request.InputStream获取POST表单值

C#如何从context.Request.InputStream获取POST表单值,c#,http,parsing,post,C#,Http,Parsing,Post,我想用c#制作一个应用程序,它可以接收发送给它的任何帖子。 例如,当我从html发布时,应该将inp1作为某物。 到目前为止,我一直在寻找每一个问题,并且找到了这个问题 private void ProcessRequest(HttpListenerContext context) { // Get the data from the HTTP stream var body = new Stream

我想用c#制作一个应用程序,它可以接收发送给它的任何帖子。 例如,当我从html发布
时,应该将inp1作为某物。 到目前为止,我一直在寻找每一个问题,并且找到了这个问题

    private void ProcessRequest(HttpListenerContext context)
            {
                // Get the data from the HTTP stream
                var body = new StreamReader(context.Request.InputStream).ReadToEnd();

                byte[] b = Encoding.UTF8.GetBytes("ACK");
                context.Response.StatusCode = 200;
                context.Response.KeepAlive = false;

                NameValueCollection coll = HttpUtility.ParseQueryString(body);
                Debug.Write(body);
}
以下是输出:

------WebKitFormBoundaryDykOSF9gSjwBSbS2
Content-Disposition: form-data; name="inp1"

something
------WebKitFormBoundaryDykOSF9gSjwBSbS2--

如何获得像
“form[“inp1”]=something“

这样的纯格式,而不是扔掉
col1
,使用它获取集合中的所有键和值,并将它们格式化为字符串以供输出。以下是一个例子:

 NameValueCollection coll = HttpUtility.ParseQueryString(body);
 var response = new StringBuilder();
 foreach (var key in col1.AllKeys)
 {
     response.Append(String.Format(@"form[""{0}""] = {1}\r\n", key, col1[key]));  //Edit the format string to taste
 }
 Debug.Write(response.ToString());

不要丢弃
col1
,而是使用它获取集合中的所有键和值,并将它们格式化为字符串以供输出。以下是一个例子:

 NameValueCollection coll = HttpUtility.ParseQueryString(body);
 var response = new StringBuilder();
 foreach (var key in col1.AllKeys)
 {
     response.Append(String.Format(@"form[""{0}""] = {1}\r\n", key, col1[key]));  //Edit the format string to taste
 }
 Debug.Write(response.ToString());

很抱歉迟发评论,但非常感谢,它真的帮助了我,,,抱歉迟发评论,但非常感谢,它真的帮助了我,,,