February 26, 2009
Helping junior developers

Although according to my contract I am meant to be a Junior IT analyst (only god knows what that actually means) I have been recently coaching some junior developers in C#. Today we had to do some work with SSL certificates with one of the service we have working.

The most interesting thing of working with people that are not yet OO developers is that the try to understand everything and abstracting away from things seems very hard for them. This happens a lot with C# which has a lot of things going on related with the OS and that is does not tell you about and a perfect example is the use of a HttpWebRequest when working with SSL. The following code performs all the required operations to send an email using one of our services at Electrabel (we do that for security reasons, we do not let apps to use the SMTP server directly)


using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Text;

namespace HttpsExample {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(@"This application shown the use of SSL in .Net");
            /* to be able to access the webserrvice using ssl we are going to rely 
             * on the HTTPS request object that way we do not have to worry 
             * about the certiciates as long as they have been added to the system.
             */
            // we set the ulr of the request.
            string url = "https:///path/to/MailServiceSOAP";
            HttpWebRequest mailRequest = (HttpWebRequest)WebRequest.Create(url);
            /* once we have the request we set the data that will use for the message 
             * at this point we have to set several things:
             * Method: Sets the method used of the request. GET retrieves data from 
             * the URL,  POST sends data to the url.
             * ContentType: Sets the content type being send to the service. We 
             * can choose between several ones but when sending xml we have to 
             * use text/xml
             * ContentLength: The default values of this property is -1 which tell 
             * that no data is going to be send, this in our case is wrong, we 
             * should set it to the size of the xml to send.
             * Stream: We need to set the stream that will be send to the object.
             * Credentials: This is atricky part, we need to set up the credentials 
             * otherwise we wont be able to work :P
             */
            //we set the data to be sent, of course this is just and example
            string message = @"The email that will be sent is:
From: email1
to: email2;email3;
body:
This email has been sent to you because someone used the console application that 
Manuel wrote to explain the use of the mail service using .Net. If no one told you you 
ere going to receive this, blame Manuel, becuase he is the one that used 
your addresses.";

            message = @"
            <request>
	            <from>email1</from>
	            <to>
		            <email>email2</email>
		            <email>email3</email>
	            </to>
	            <subject>Testing the mail service</subject>
	            <bodyFormat>text/plain</bodyFormat>
	            <body>This email has been sent to you because someone 
                     used the console application that Manuel wrote to explain the 
                     use of the mail service using .Net. If no one told you you were 
                     going to receive this, blame Manuel, because he is the one 
                     that used our addresses.
	            </body>
            </request>";
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] data = encoding.GetBytes(message);

            mailRequest.Method = "POST";
            mailRequest.ContentType = "text/xml";
            mailRequest.ContentLength = data.Length;
            try {
                Stream postStream = mailRequest.GetRequestStream();
                postStream.Write(data, 0, data.Length);
                postStream.Flush();
                postStream.Close();

                // we get the response:
                StreamReader streamReader = 
                      new StreamReader(
                      mailRequest.GetResponse().GetResponseStream());
                Console.WriteLine("The response from the service was:" 
                      + streamReader.ReadToEnd());
            } catch (WebException e) {
                // This is just an EXAMPLE!!!!!
                Console.WriteLine("The following exception "
                + "was thrown when trying to use the sevice:\n" + e.Message);
            }
            Console.ReadLine();
        }
    }
}


Working with the .Net API is great, because, as you can see in this code, we do not give a rats ass about how the SSL certificates are managed. The HttpWebReqest does all the work for us:

  1. Checks all the certificates installed in the machine.
  2. Talks with the service about the SSL certificate and perform the exchange.
  3. Throws an exception if things went bad, otherwise we can access the response from the server.

I tried to explain it this to the developers and they did not understand it until they got the code an played with it…

PS:The code is an example and will not work because you are not pointing to the mail service, but it is good to illustrate what I mean.

12:12pm  |   URL: http://tmblr.co/Zn5Hby4tatb
Filed under: c code programming .net SSL OO