using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Net.Mail; using System.Collections; using System.Collections.Specialized; using System.Net; using System.Xml; using System.Text; public class ConstantContact { public ConstantContact() { } public string AddContact(int listID, string emailAddress, string firstName, string lastName, string addressLine1, string addressLine2, string city, string statecode, string statename, string zip, string homePhone, string customField1) { if (listID < 0) listID = 1; string Username = Configuration.Get("ConstantContact.Username"); string Password = Configuration.Get("ConstantContact.Password"); string APIKey = Configuration.Get("ConstantContact.APIKey"); string ContactURI = "https://api.constantcontact.com/ws/customers/" + Username + "/contacts"; // Note: Remember to add "using System.Net;" // Create LoginCreditials CredentialCache LoginCredentials = new CredentialCache(); // Add a new credential for this account LoginCredentials.Add( new Uri("https://api.constantcontact.com/ws/customers/" + Username), // Set up URI for API site "Basic", // Basic login type new NetworkCredential( APIKey + "%" + Username, // Set up API Username (APIKey%Username) Password)); // Password // Create WebRequest HttpWebRequest Request = (HttpWebRequest)WebRequest.Create( ContactURI); // URI for the GET // Set Request to be a POST Request.Method = "POST"; // Set the ContentType property. Request.ContentType = "application/atom+xml"; // Set Request credentials Request.Credentials = LoginCredentials; // Set up XML String for the POST // Long string with quotes, use an absolute string with literals string XMLData = " "; // Set up the XML Document, application dependant StringBuilder str = new StringBuilder(); str.Append(""); str.Append(" "); str.Append("" + " 2008-07-23T14:21:06.407Z" + ""); str.Append(""); str.Append("data:,none"); str.Append("Contact"); str.Append(""); str.Append(""); if (emailAddress != "") str.Append("" + emailAddress + ""); if (firstName != "") str.Append("" + firstName + ""); if (lastName != "") str.Append("" + lastName + ""); if (homePhone != "") str.Append("" + homePhone + ""); if (addressLine1 != "") str.Append("" + addressLine1 + ""); if (addressLine2 != "") str.Append("" + addressLine2 + ""); if (city != "") str.Append("" + city + ""); if (statecode != "") str.Append("" + statecode + ""); if (statename != "") str.Append("" + statename + ""); if (zip != "") str.Append("" + zip + ""); if (customField1 != "") str.Append("" + customField1 + ""); str.Append("" + "ACTION_BY_CUSTOMER" + ""); str.Append(""); str.Append(""); str.Append(""); str.Append(""); str.Append(""); str.Append(""); XMLData = str.ToString(); // Convert XMLData to byteArray for posting byte[] byteArray = Encoding.UTF8.GetBytes(XMLData); // Send POST request // Recast the response to HttpWebResponse for easier processing // Place in a try block to ensure that any errors are caught try { // Set the ContentLength portion of the header Request.ContentLength = byteArray.Length; string XMLResponse = "Bytes to send: " + byteArray.Length; // Create a stream for the POST Request Stream streamRequest = Request.GetRequestStream(); // Write the data to the stream. streamRequest.Write(byteArray, 0, byteArray.Length); streamRequest.Close(); HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); // Process the Response as needed // This is a generic StreamReader to read the entire response in // You can recast this as any type of stream derivative // Note: Remember to add "using System.IO" StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string // Note there is no XML response for a successful POST XMLResponse += Response.StatusCode + " " + Response.StatusDescription + " " + Reader.ReadToEnd(); // Close Reader Reader.Close(); // Close the response to free up the system resources Response.Close(); return XMLResponse; } catch (WebException e) { // Get the web exception type and response code return "WebException: " + e.Status + " With response: " + e.Message; } catch (Exception e) { // Get the exception type return "Exception:" + e.Message; } } // XmlNode baseNode = GetXmlResponse_Post(TestURL + "Rate", postData); public XmlNode GetXmlResponse_Post(string url, string postData) { StreamWriter myWriter = null; HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); objRequest.Method = "POST"; objRequest.ContentLength = postData.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(postData); myWriter.Close(); HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(objResponse.GetResponseStream()); objResponse.Close(); return xmlDoc; } }