Change HttpContext.Request.InputStream
<p>I am getting lot of errors for <strong>HttpRequestValidationException</strong> in my event log.  </p>

<p>Is it possible to HTMLEncode all the inputs from override of ProcessRequest on web page. I have tried this but it gives context.Request.InputStream.CanWrite == false always.  </p>

<p>Is there any way to HTMLEncode all the feilds when request is made?</p>

<pre><code>public override void ProcessRequest(HttpContext context)
            {
                if (context.Request.InputStream.CanRead)
                {
                    IEnumerator en = HttpContext.Current.Request.Form.GetEnumerator();
                    while (en.MoveNext())
                    {
                        //Response.Write(Server.HtmlEncode(en.Current + " = " +
                        //HttpContext.Current.Request.Form[(string)en.Current]));
                    }

                    long nLen = context.Request.InputStream.Length;
                    if (nLen &gt; 0)
                    {
                        string strInputStream = string.Empty;

                        context.Request.InputStream.Position = 0;
                        byte[] bytes = new byte[nLen];
                        context.Request.InputStream.Read(bytes, 0, Convert.ToInt32(nLen));
                        strInputStream = Encoding.Default.GetString(bytes);
                        if (!string.IsNullOrEmpty(strInputStream))
                        {
                            List&lt;string&gt; stream = strInputStream.Split('&amp;').ToList&lt;string&gt;();
                            Dictionary&lt;int, string&gt; data = new Dictionary&lt;int, string&gt;();
                            if (stream != null &amp;&amp; stream.Count &gt; 0)
                            {
                                int index = 0;
                                foreach (string str in stream)
                                {
                                    if (str.Length &gt; 3 &amp;&amp; str.Substring(0, 3) == "txt")
                                    {
                                        string textBoxData = str;
                                        string temp = Server.HtmlEncode(str);
                                        //stream[index] = temp;
                                        data.Add(index, temp);
                                        index++;
                                    }
                                }

                                if (data.Count &gt; 0)
                                {
                                    List&lt;string&gt; streamNew = stream;
                                    foreach (KeyValuePair&lt;int, string&gt; kvp in data)
                                    {
                                        streamNew[kvp.Key] = kvp.Value;
                                    }

                                    string newStream = string.Join("", streamNew.ToArray());

                                    byte[] bytesNew = Encoding.Default.GetBytes(newStream);

                                    if (context.Request.InputStream.CanWrite)
                                    {

                                        context.Request.InputStream.Flush();
                                        context.Request.InputStream.Position = 0;
                                        context.Request.InputStream.Write(bytesNew, 0, bytesNew.Length);
                                        //Request.InputStream.Close();
                                        //Request.InputStream.Dispose();
                                    }
                                }
                            }
                        }
                    }
                }


                base.ProcessRequest(context);
            }
</code></pre>
