This code is set up for a contact, but is easily adapted for any of the REST APIs that exist.
GET:
// 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, this is a factory class with a static constructor
// You can not use new, all requests must be created with WebRequest.Create
WebRequest Request = WebRequest.Create(ContactURI); // URI for the GET
// Set Request credentials
Request.Credentials = LoginCredentials;
// Send GET request
// Recast the response to HttpWebResponse for correct processing
// Place in a try block to ensure that any errors are caught
try
{
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
// Process the Response as needed
// This is a generic StreamReader to read the entire response in
// GetResponseStream can be recast to any of the Stream types
// Note: Remember to add using System.IO
StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string
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, error number returned here
return WebException: + e.Status + + e.Message;
}
catch (Exception e)
{
// Get the exception type
return Exception: + e.Message;
}
PUT:
// 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 PUT
// Set Request to be a PUT
Request.Method = PUT;
// Set the ContentType property.
Request.ContentType = application/atom+xml;
// Set Request credentials
Request.Credentials = LoginCredentials;
// Set up XML String for the PUT
// Long string with quotes, use an absolute string with literals
string XMLData = ;
// Set up the XML Document, application dependant
// Convert XMLData to byteArray for posting
byte[] byteArray = Encoding.UTF8.GetBytes(XMLData);
// Send PUT 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 PUT Request
Stream streamRequest = Request.GetRequestStream();
// Write the data to the stream.
// Note: Remember to add using System.IO
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
StreamReader Reader = new StreamReader(Response.GetResponseStream());
// Read the entire XML response to a string
// Note there may not be a XML response for a successful PUT
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;
}
POST:
// 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 POST
// 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 or a StringBuilder
XMLData = ;
// Set up the XML Document, application dependant
// 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
// Note: Remember to add using System.IO
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
StreamReader Reader = new StreamReader(Response.GetResponseStream());
// Read the entire XML response to a string
// Note there may not be a 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;
}
Please feel free to post comments, corrections or suggestions.
I've updated the code to
I've updated the code to correctly reflect Basic Authentication over HTTPS.
Dave B Support Engineer, Constant Contact
Failed Attempt...
Hello! I have attempted to adapt your code to get the campaign information, yet am getting errors.
static void Main(string[] args)
{
string APIKey = "myAPIKeyHere"; // Obviously Correct API Key
string myUsername = "Reelix";
string myPassword = "myPassOverHere"; // Obviously Correct Pass
string myString = getDetails(APIKey, myUsername, myPassword);
Console.WriteLine(myString);
Console.ReadLine();
}
public static string getDetails(string APIKey, string myUsername, string myPassword)
{
CredentialCache LoginCredentials = new CredentialCache();
LoginCredentials.Add(new Uri("http://api.constantcontact.com/ws/customers/" + myUsername), "Basic", new NetworkCredential(APIKey + "%" + myUsername, myPassword));
WebRequest Request = WebRequest.Create("http://api.constantcontact.com/ws/customers/" + myUsername + "/campaigns");
Request.Credentials = LoginCredentials;
string XMLResponse;
try
{
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string
XMLResponse = Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();
Reader.Close();
Response.Close();
return XMLResponse;
}
catch (WebException e)
{
return ("WebExecption: " + e.Status + "\n" + e.Message);
}
catch (Exception e)
{
return ("General Exception: " + e.Message);
}
}
The code compiles properly, but always returns: WebExecption: ProtocolError The remote server returned an error: (401) Unauthorized. Am I doing something wrong? - Reelix
It looks like you are not
It looks like you are not using the correct protocol for your Authentication. If you read our documentation on Authentication here, you'll notice that it requires you to use HTTPS for all requests. Not using HTTPS will always result on a 401 as it will not be able to Authenticate you. Changing the following lines will fix your code and it ran fine for me:
LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + myUsername), "Basic", new NetworkCredential(APIKey + "%" + myUsername, myPassword));
WebRequest Request = WebRequest.Create("https://api.constantcontact.com/ws/customers/" + myUsername + "/campaigns");
Dave B Support Engineer, Constant Contact
Hi Dave! My mistake! Your
Hi Dave!
My mistake! Your changes worked perfectly!
Thanks alot, and enjoy your day!
- Reelix