title: Debugging an HttpWebResponse
body: <p>This is being done on a Windows Forms App. I've spent a ton of time stepping through this code with the debugger. What I've found are the following things and they all seem to be at this line:</p>
  
  <pre><code>using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  </code></pre>
  
  <p>1. If I include</p>
  
  <pre><code>request.SendChunked = true;
  </code></pre>
  
  <p>I get this error at the response line previously stated:</p>
  
  <pre><code>'System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.
  </code></pre>
  
  <p>2. If I comment out the code in #1, I receive this error at that main response line that I mentioned in the beginning:</p>
  
  <pre><code>'System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
  </code></pre>
  
  <p>3. If I go with route #1, the "Connection" of the request remains as "KeepAlive" all the way through. But if I go with route #2, the "Connection" of the request changes to "null" at the response line that I mentioned in the beginning. </p>
  
  <pre><code>    private void HttpPost()
      {
  
  
          HttpWebRequest request = null;
          Uri uri = new Uri("https://post.craigslist.org/bulk-rss/post");
  
          request = (HttpWebRequest)WebRequest.Create(uri);
          request.Method = "POST";
          request.ContentType = "application/x-www-form-urlencoded";
          XmlDocument doc = new XmlDocument();
          doc.Load("XMLFile1.xml");
          //request.ContentLength = doc.InnerXml.Length;
  
          request.SendChunked = true;
          using (Stream writeStream = request.GetRequestStream())
          {
              UTF8Encoding encoding = new UTF8Encoding();
              byte[] bytes = encoding.GetBytes(doc.InnerXml);
              //request.ContentLength = bytes.Length;
              writeStream.Write(bytes, 0, bytes.Length);
          }
  
  
          string result = string.Empty;
  
          request.ProtocolVersion = System.Net.HttpVersion.Version11;
          request.KeepAlive = false;
          try
          {
              using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
              {
                  using (Stream responseStream = response.GetResponseStream())
                  {
                      using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
                      {
                          result = readStream.ReadToEnd();
                      }
                  }
  
              }
  
          }
          catch (Exception e)
          {
              string innerException = String.Format("Inner exception: '{0}'", e.Data);
              string exceptionCause = String.Format("An error occurred: '{0}'", e);
              System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\DebugOutputFile\exception.txt", exceptionCause);
              System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\DebugOutputFile\innerException.txt", innerException);
  
          }
      }
  </code></pre>
  
  <p>I feel like these things are adding up towards a solution, but I could really use some guidance.</p>