I've seen this topic many times, but none of the solutions have helped me and I don't know why nothing works.
I have a C# web part, and I am simply trying to read the data content of an http post request. In the request is some xml, but this doesn't show up when I try to read it in the web part. it's only giving me the header data and some server variables.
The request I'm trying to read is submitted through the Simple Rest extension for chrome. When I monitor with fiddler, I can see the request, and when I click on TextView, I can see all the XML no problem.. So why doesn't it show up on the server?
I tried using Context.Request.SaveAs()
, but that appears to only give me the header data. I also tried looping through the Context.Request.Params
and printing each parameter, but none of them contain the xml.
Finally, I tried reading the content of the request using
Context.Request.ContentEncoding
.GetString(Context.Request.BinaryRead(Context.Request.TotalBytes))
and also
string strmContents = "";
using (StreamReader reader = new StreamReader(Context.Request.InputStream))
{
while (reader.Peek() >= 0)
{
strmContents += reader.ReadLine();
}
}
but both of those methods result in empty strings.
What really confuses (and aggravates) me is that if I look at the Context.Request.ContentLength
, it is the same as the number of characters in my XML! I know the content is getting passed, but I don't know how to access it.
I feel bad for posting this, because the code isnt mine, but i cannot find the original post.
This extension method has been extremely useful for me, you simply use it like this: HttpContext.Current.Request.ToRaw();
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;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With