Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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# 从C中的context.httprequest读取原始数据#_C#_Xml_Http_Post_Request - Fatal编程技术网

C# 从C中的context.httprequest读取原始数据#

C# 从C中的context.httprequest读取原始数据#,c#,xml,http,post,request,C#,Xml,Http,Post,Request,这个话题我已经看过很多次了,但是没有一个解决方案对我有帮助,我也不知道为什么什么都不管用 我有一个C#web部件,我只是试图读取http post请求的数据内容。请求中有一些xml,但当我试图在web部件中阅读它时,它没有显示出来。它只给我标题数据和一些服务器变量 我试图阅读的请求是通过chrome的简单Rest扩展提交的。当我使用fiddler进行监视时,我可以看到请求,当我单击TextView时,我可以看到所有的XML,没有问题。。那么为什么它不显示在服务器上呢 我尝试使用Context.R

这个话题我已经看过很多次了,但是没有一个解决方案对我有帮助,我也不知道为什么什么都不管用

我有一个C#web部件,我只是试图读取http post请求的数据内容。请求中有一些xml,但当我试图在web部件中阅读它时,它没有显示出来。它只给我标题数据和一些服务器变量

我试图阅读的请求是通过chrome的简单Rest扩展提交的。当我使用fiddler进行监视时,我可以看到请求,当我单击TextView时,我可以看到所有的XML,没有问题。。那么为什么它不显示在服务器上呢

我尝试使用
Context.Request.SaveAs()
,但这似乎只提供了标题数据。我还尝试循环遍历
Context.Request.Params
并打印每个参数,但它们都不包含xml

最后,我尝试使用

Context.Request.ContentEncoding                     
       .GetString(Context.Request.BinaryRead(Context.Request.TotalBytes))
而且

string strmContents = "";
using (StreamReader reader = new StreamReader(Context.Request.InputStream))
{
    while (reader.Peek() >= 0)
    {
       strmContents += reader.ReadLine();
    }
}
但这两种方法都会导致空字符串


真正让我困惑(并加剧)的是,如果我查看
Context.Request.ContentLength
,它与XML中的字符数相同!我知道内容正在通过,但我不知道如何访问它。

我为发布此内容感到难过,因为代码不是我的,但我找不到原始帖子

这种扩展方法对我非常有用,您只需像这样使用它: HttpContext.Current.Request.ToRaw()

使用System.IO;
使用System.Web;
使用log4net;
命名空间System.Web.ExtensionMethods
{
/// 
///HTTP请求的扩展方法。
/// 
///请参阅HTTP 1.1规范http://www.w3.org/Protocols/rfc2616/rfc2616.html
///有关实施决策的详细信息。
/// 
/// 
公共静态类HttpRequestExtensions
{
/// 
///将原始http请求转储为字符串。
/// 
///应该扔掉的垃圾。
///原始HTTP请求。
公共静态字符串ToRaw(此HttpRequest请求)
{
StringWriter编写器=新的StringWriter();
WriteStartLine(请求、编写器);
编写者(请求、编写者);
WriteBook(请求、编写器);
返回writer.ToString();
}
公共静态字符串GetBody(此HttpRequest请求)
{
StringWriter编写器=新的StringWriter();
WriteBook(请求、编写器);
返回writer.ToString();
}
私有静态void WriteStartLine(HttpRequest请求,StringWriter编写器)
{
常量字符串空格=”;
writer.Write(request.HttpMethod);
writer.Write(SPACE+request.Url);
writer.WriteLine(SPACE+request.ServerVariables[“SERVER_协议]);
}
私有静态无效写头(HttpRequest请求、StringWriter编写器)
{
foreach(request.Headers.AllKeys中的字符串键)
{
WriteLine(string.Format(“{0}:{1}”,key,request.Headers[key]);
}
writer.WriteLine();
}
私有静态void编写器(HttpRequest请求,StringWriter编写器)
{
StreamReader=新的StreamReader(request.InputStream);
尝试
{
字符串体=reader.ReadToEnd();
作者。作者行(正文);
}
最后
{
reader.BaseStream.Position=0;
}
}
}
}
只要你的帖子是在帮助地球上某个地方的人,就永远不要感觉不好。
using System.IO;
using System.Web;
using log4net;

namespace System.Web.ExtensionMethods
{
    /// <summary>
    /// Extension methods for HTTP Request.
    /// <remarks>
    /// See the HTTP 1.1 specification http://www.w3.org/Protocols/rfc2616/rfc2616.html
    /// for details of implementation decisions.
    /// </remarks>
    /// </summary>
    public static class HttpRequestExtensions
    {

    /// <summary>
    /// Dump the raw http request to a string. 
    /// </summary>
    /// <param name="request">The <see cref="HttpRequest"/> that should be dumped.               </param>
    /// <returns>The raw HTTP request.</returns>
    public static string ToRaw(this HttpRequest request)
    {
        StringWriter writer = new StringWriter();

        WriteStartLine(request, writer);
        WriteHeaders(request, writer);
        WriteBody(request, writer);

        return writer.ToString();
    }

    public static string GetBody(this HttpRequest request)
    {
        StringWriter writer = new StringWriter();
        WriteBody(request, writer);

        return writer.ToString();
     }

     private static void WriteStartLine(HttpRequest request, StringWriter writer)
     {
         const string SPACE = " ";

          writer.Write(request.HttpMethod);
          writer.Write(SPACE + request.Url);
          writer.WriteLine(SPACE + request.ServerVariables["SERVER_PROTOCOL"]);
     }


     private static void WriteHeaders(HttpRequest request, StringWriter writer)
     {
            foreach (string key in request.Headers.AllKeys)
            {
                   writer.WriteLine(string.Format("{0}: {1}", key, request.Headers[key]));
            }

            writer.WriteLine();
      }


      private static void WriteBody(HttpRequest request, StringWriter writer)
      {
           StreamReader reader = new StreamReader(request.InputStream);

            try
            {
                string body = reader.ReadToEnd();
                writer.WriteLine(body);
            }
            finally
            {
                reader.BaseStream.Position = 0;
            }
        }
}
}