title: C# HTTPS request with post
body: <p>I am hoping for an complete example answer for HTTPS like the answer to 
  <a href="https://stackoverflow.com/questions/4015324/http-request-with-post">HTTP request with post</a> especially Method 3. Specifically including the security policy used in context. Both for when there is no certificate and when their is. In my example, I have no real certificate, but must use HTTPS.
  I wonder if I have another beginner error. Code is below (links changed to fakes)
  I followed the steps for HTTP from here: <a href="https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx</a></p>
  
  <p>Other answers about HTTPS posts, both in StackOverFlow and outside either point to an obsolete security class or are <a href="https://stackoverflow.com/questions/5648596/call-webservice-from-c-sharp-application-using-ssl">only a partial example</a>. And I think other beginners would  also reference a more current answer and complete example.</p>
  
  <p>The C# code here gets a 500 returned, but I need to be confident my security policy call is not the reason.<br>
  Thanks for your help</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Net;
  using System.Net.Security;
  using System.Text;
  using System.Security.Cryptography.X509Certificates;
  
  namespace GradeSync
  
  {
  
  
  /* HTTP POST
  
  The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
  
  REQUEST:
  POST /webservices/PspServices.asmx/UpdateGradingSheet HTTP/1.1
  Host: itech.psp.org
  Content-Type: application/x-www-form-urlencoded
  Content-Length: length
  
  UserId=string&amp;LessonId=string&amp;Marks=string&amp;ClassId=string
  
  RESPONSE: 
  HTTP/1.1 200 OK
  Content-Type: text/xml; charset=utf-8
  Content-Length: length
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;string xmlns="http://tempuri.org/"&gt;string&lt;/string&gt;
   */
  
  
  
      public class PostGradesViaWebRequestPost
      {
          // https://stackoverflow.com/questions/5648596/call-webservice-from-c-sharp-application-using-ssl
  
  
          /// public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
          ///{
          ///    Console.WriteLine(sslPolicyErrors);  // Or whatever you want to do...
          ///    return true;
          ///}
  
          public static void Main()
          {
  
          string gradesURI = 
  
          "https://itech.psp.org/webservices/PspServices.asmx/UpdateGradingSheet";
  
          string userId  = "300810060";   // 300810060 = Dorinda Bex ; 300835525 = Rachel Bess
          string lesson  = "L1";          // Lesson #
          string points  =  "9";          // Review score
          string classId = "432462";     // 432462 = Independent Study 2014 - Sec.2 ; 432525 = Modesto, CA Spring 2015
  
          // https://stackoverflow.com/questions/12506575/how-to-ignore-the-certificate-check-when-ssl
          System.Net.ServicePointManager.ServerCertificateValidationCallback +=
              delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                      System.Security.Cryptography.X509Certificates.X509Chain chain,
                      System.Net.Security.SslPolicyErrors sslPolicyErrors)
              {
                  return true; // **** Always accept
              };
          //// goes with policy System.Net.ServicePointManager.ServerCertificateValidationCallback = policy.ValidateServerCertificate;
  
          //https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
  
          // 1  Create a request using a URL that can receive a post. 
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gradesURI);  /// new
          //xxWebRequest rqst = HttpWebRequest.Create(gradesURI);
  
          //WebRequest request = WebRequest.Create( gradesURI );
  
          // 2
          request.Credentials = CredentialCache.DefaultCredentials;
          request.ProtocolVersion = HttpVersion.Version11;
          /// not needed?  ((HttpWebRequest)request).UserAgent = ".NET Framework Exa
          /// mple Client";
  
          // 3 Set the Method property of the request to POST.
          request.Method = "POST";
  
          // 4 Set the ContentLength property of the WebRequest.
          // Create POST data and convert it to a byte array.
          string postData = "Userid="+userId + "&amp;Lesson=" + lesson + "&amp;Marks="+points + "&amp;ClassId="+classId;
          byte[] byteArray = Encoding.UTF8.GetBytes (postData);
  
          // 5 Set the ContentType property of the WebRequest.
          request.ContentType = "application/x-www-form-urlencoded";   // OK right type
  
          //  Set the ContentLength property of the WebRequest.
          request.ContentLength = byteArray.Length;
  
          // 6 Get the request stream.
          Stream dataStream = request.GetRequestStream ();
  
          // 7 Write the data to the request stream.
          dataStream.Write (byteArray, 0, byteArray.Length);
  
          // 8 Close the Stream object.
          dataStream.Close ();
  
  
          //  Response  /////////////////////////////////////////////
          // 9 Get the response.
          WebResponse response = request.GetResponse();
  
          // 10 Display the status
          Console.WriteLine (((HttpWebResponse)response).StatusDescription);
  
          // 11 Get the stream containing content returned by the server.
          dataStream = response.GetResponseStream ();   // from example
  
          // Open the stream using a StreamReader for easy access.
          StreamReader reader = new StreamReader (dataStream);
          // Read the content.
          string responseFromServer = reader.ReadToEnd ();
          // Display the content.
          Console.WriteLine (responseFromServer);
  
          // Clean up the streams.
          reader.Close ();
          dataStream.Close ();
  
          // 12
          response.Close();
  
  
          }  // end main
      }  // end class
  }  // end namespace
  </code></pre>