0

I am hoping for an complete example answer for HTTPS like the answer to HTTP request with post 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: https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx

Other answers about HTTPS posts, both in StackOverFlow and outside either point to an obsolete security class or are only a partial example. And I think other beginners would also reference a more current answer and complete example.

The C# code here gets a 500 returned, but I need to be confident my security policy call is not the reason.
Thanks for your help

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&LessonId=string&Marks=string&ClassId=string

RESPONSE: 
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
 */



    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 + "&Lesson=" + lesson + "&Marks="+points + "&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
Community
  • 1
  • 1
traisen
  • 53
  • 1
  • 2
  • 10

1 Answers1

2

Turns out because the https is on the server side, no policy or security code is needed at all! So I'm able to use Method 3 of HTTP request with post

My problem was typo (Lesson not LessonId), but as REST beginner I assumed it was the HTTPS and security policy.

Now wondering when a security certificate is needed? (Probably answered) But this probably better question. Beginners need a hint to get to right question.

Community
  • 1
  • 1
traisen
  • 53
  • 1
  • 2
  • 10