Authenticated POST with C#

Want to connect to IMified using C# and the .Net Framework but having trouble sending HTTP Basic Authentication? These sample functions from Jim Burns (jim@sigdev.co.uk) should help.

  public XmlDocument GetAllUsers()
   {
       XmlDocument theXml = null;
       if (iUrl == null)
       {
           throw new Exception("You must set the property,
              IMIFiedURL, before calling this method.");
       }

       HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(iUrl);

       String postData = string.Empty;
       postData += "&botkey=" + botKey;
       postData += "&apimethod=" + "getallusers";

       ASCIIEncoding encoding = new ASCIIEncoding();
       byte[] byte1 = encoding.GetBytes(postData);
       hwr.ContentLength = byte1.Length;

       String base64authString = base64Encode(username + ":" + password);
       base64authString = "Basic " + base64authString;
       hwr.Headers.Add("AUTHORIZATION", base64authString);

       hwr.Method = "POST";
       hwr.ContentType = "form-data";

       Stream reqStream = hwr.GetRequestStream();
       reqStream.Write(byte1, 0, byte1.Length);

       HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();

       theXml = new XmlDocument();
       theXml.Load(response.GetResponseStream());
       return theXml;
   }

   private string base64Encode(string data)
   {
       try
       {
           byte[] encData_byte = new byte[data.Length];
           encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
           string encodedData = Convert.ToBase64String(encData_byte);
           return encodedData;
       }
       catch (Exception e)
       {
           throw new Exception("Error in base64Encode" + e.Message);
       }
   }