using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Net; using System.IO; using System.Web; namespace AddCampaign { class AddCampaign { static void Main(string[] args) { // Establish the request WebRequest webRequest = WebRequest.Create ("http://www.agendize.com/api/1.0/data"); // the xml data to send String xmlData = "Campagne0#EC50FFThis is a campaign uploaded by c#"; // Set the authentication string authInfo = "myusername:mypassword"; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); webRequest.Headers["Authorization"] = "Basic " + authInfo; // Set the http request in POST method webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST"; // Build the query string String queryString = "key=3ac024abf8c72460764b965199f81808f4cb08&xml="+urlEncode(xmlData); byte[] bytes = Encoding.ASCII.GetBytes (queryString); Stream os = null; webRequest.ContentLength = bytes.Length; //Count bytes to send os = webRequest.GetRequestStream(); os.Write (bytes, 0, bytes.Length); //Send it // get the response HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(), enc); string xmlContent = responseStream.ReadToEnd(); webResponse.Close(); responseStream.Close(); // Retrieve AgendiZe response code obtained by searching in the parsed xml content XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlContent); XmlNodeList nodes = xmlDoc.GetElementsByTagName("result"); // Display the response code Console.WriteLine(nodes.Item(0).InnerText); } // Sometimes Visual Strudio can't get the HttpUtility class that's why we provide you our own urlEncode() method static String urlEncode(string toEncode) { char[] toEncodeCharacters = {'$','&','+',',','/',':',';','=','?','@',' ','\"','<','>','#','%'}; String[] hexCodedChar = { "24", "26", "2B", "2C", "2F", "3A", "3B", "3D", "3F", "40" ,"20","22","3C","3E","23","25"}; String encoded = ""; for (int i = 0; i < toEncode.Length; i++) { char currentChar = toEncode.ElementAt<char>(i); int indice = -1; for(int j = 0; j < toEncodeCharacters.Length;j++) if (currentChar == toEncodeCharacters.ElementAt<char>(j)) indice = j; if (indice == -1) encoded += currentChar; else encoded += "%" + hexCodedChar.ElementAt<string>(indice); } return encoded; } } }