title: Could not post on WebRequest if the Content Length > 7kb using C#
body: <p>I am trying to post a Json to our web service. My problem is that when my request.ContentLength exceeds 7kb. I will get a web exception 500 at request.GetResponse(). But if my request.ContentLength is below 7kb, my post is successfully sent. </p>
  
  <p>Error Message:</p>
  
  <pre><code> The remote server returned an error: (500) Internal Server Error.
  </code></pre>
  
  <p>Source code:</p>
  
  <pre><code>public static string JsonPost(string url, string method, string postData)
  {
      Uri address = new Uri(url + method);
      HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
      request.Method = "POST";
      request.ContentType = "application/json";
      byte[] byteData = UTF8Encoding.UTF8.GetBytes(postData);
      request.ContentLength = byteData.Length;
      using (Stream postStream = request.GetRequestStream())
      {
          postStream.Write(byteData, 0, byteData.Length);
      }
      try
      {
          using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
          {
              StreamReader reader = new StreamReader(response.GetResponseStream());
              string JsonResponse = reader.ReadToEnd();
              return JsonResponse;
          }
      }
      catch (WebException ex)
      {
          string message = ((System.Net.HttpWebResponse)(ex.Response)).StatusDescription;
          if (message.Contains("busy"))
              return message;
          else
              throw new Exception(message);
      }
  }
  </code></pre>
  
  <p>Web.config:</p>
  
  <pre><code>        &lt;?xml version="1.0"?&gt;
  
  &lt;configuration&gt;
    &lt;appSettings&gt;
      &lt;add key="webpages:Version" value="1.0.0.0"/&gt;
      &lt;add key="ClientValidationEnabled" value="true"/&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true"/&gt;
    &lt;/appSettings&gt;
  
    &lt;system.web&gt;
      &lt;httpRuntime executionTimeout="180" maxRequestLength="1048576" useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
      minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true" /&gt;
  
      &lt;compilation debug="true" targetFramework="4.0"&gt;
        &lt;assemblies&gt;
          &lt;add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
          &lt;add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
          &lt;add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
          &lt;add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
          &lt;add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
        &lt;/assemblies&gt;
      &lt;/compilation&gt;
  
      &lt;authentication mode="Forms"&gt;
        &lt;forms loginUrl="~/Home/" cookieless="AutoDetect" timeout="2880" /&gt;
      &lt;/authentication&gt;
  
      &lt;membership&gt;
        &lt;providers&gt;
          &lt;clear/&gt;
          &lt;add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
               enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
               maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
               applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/membership&gt;
  
      &lt;profile&gt;
        &lt;providers&gt;
          &lt;clear/&gt;
          &lt;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/profile&gt;
  
      &lt;roleManager enabled="false"&gt;
        &lt;providers&gt;
          &lt;clear/&gt;
          &lt;add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /&gt;
          &lt;add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/roleManager&gt;
  
      &lt;pages&gt;
        &lt;namespaces&gt;
          &lt;add namespace="System.Web.Helpers" /&gt;
          &lt;add namespace="System.Web.Mvc" /&gt;
          &lt;add namespace="System.Web.Mvc.Ajax" /&gt;
          &lt;add namespace="System.Web.Mvc.Html" /&gt;
          &lt;add namespace="System.Web.Routing" /&gt;
          &lt;add namespace="System.Web.WebPages"/&gt;
        &lt;/namespaces&gt;
      &lt;/pages&gt;
    &lt;/system.web&gt;
  
    &lt;system.webServer&gt;
      &lt;security&gt;
        &lt;requestFiltering&gt;
          &lt;requestLimits maxAllowedContentLength="1073741824" /&gt;
        &lt;/requestFiltering&gt;
      &lt;/security&gt;
      &lt;validation validateIntegratedModeConfiguration="false"/&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;/system.webServer&gt;
  
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Exception Logs:</p>
  
  <pre><code>    (System.Net.HttpWebResponse)(ex.Response)   {System.Net.HttpWebResponse}    System.Net.HttpWebResponse
      base    {System.Net.HttpWebResponse}    System.Net.WebResponse {System.Net.HttpWebResponse}
          CharacterSet    "ISO-8859-1"    string
          ContentEncoding ""  string
          ContentLength   1047    long
          ContentType "text/html" string
          Cookies {System.Net.CookieCollection}   System.Net.CookieCollection
          Headers {
              Access-Control-Allow-Origin: *
              Content-Length: 1047
              Cache-Control: private
              Content-Type: text/html
              Date: Tue, 18 Sep 2012 22:33:36 GMT
              Set-Cookie: ASP.NET_SessionId=belurseo1i0ureppkspetw1a; path=/; HttpOnly
              Server: Microsoft-IIS/7.0
              X-AspNet-Version: 4.0.30319
              X-Powered-By: ASP.NET
          }   System.Net.WebHeaderCollection
          IsMutuallyAuthenticated false   bool
          LastModified    {9/19/2012 6:34:07 AM}  System.DateTime
          Method  "POST"  string
          ProtocolVersion {1.1}   System.Version
          ResponseUri {http://localhost/service.svc/Update}   System.Uri
          Server  "Microsoft-IIS/7.0" string
          StatusCode  InternalServerError System.Net.HttpStatusCode
          StatusDescription   "The server encountered an error processing the request. Please see the server logs for more details."  string
          SupportsHeaders true    bool
  </code></pre>