.Net Code (VB) for adding contact - Basic Authentication

Regular Participant

.Net Code (VB) for adding contact - Basic Authentication

I've posted this code once before, but it was in digest authentication. Here are the changes I made to make it work for basic authentication. Many thanks to CC for being sooo supportive of us .Net developers...NOT. Actually thanks to peers in the forum for your direction. Code changes marked in bold. Private Sub AddToConstantContacts() Try Dim sUsername As String = "
" Dim sPassword As String = "
" Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & sUsername & "/activities" Dim sListUri As String = "https://api.constantcontact.com/ws/customers/" & sUsername & "/lists/4" Dim sAPIKey As String = "b0ae02ef-c5ba-44da-8981-32f509c86d11" 'setup httpWebReqeust to send the data Dim theLoginCredentials As CredentialCache = New CredentialCache() theLoginCredentials.Add(New Uri("https://api.constantcontact.com/ws/customers/" & sUsername), "Basic", New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)) Dim address As New Uri(sUri) Dim theRequest As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest) theRequest.Credentials = theLoginCredentials 'New NetworkCredential((sAPIKey & "%" & sUsername), sPassword) theRequest.Method = "POST" theRequest.ContentType = "application/x-www-form-urlencoded" 'Build an encoded string of the data to pass to constant contact. Dim data As New StringBuilder() data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8)) data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name" & Chr(10)), Encoding.UTF8)) data.Append(HttpUtility.UrlEncode((txtEmail.Text & ",HTML," & txtFirstName.Text & "," & txtLastName.Text), Encoding.UTF8)) data.Append("&lists=" + HttpUtility.UrlEncode(sListUri)) 'guts of the code... Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString) Dim strResponse As String = String.Empty theRequest.ContentLength = byteData.Length Using postStream As Stream = theRequest.GetRequestStream() postStream.Write(byteData, 0, byteData.Length) End Using Using theResponse As HttpWebResponse = TryCast(theRequest.GetResponse(), HttpWebResponse) Dim reader As New StreamReader(theResponse.GetResponseStream()) strResponse = reader.ReadToEnd() End Using Catch ex As Exception Throw End Try End Sub

1 REPLY 1
Moderator

Thanks for posting the sample code.  This code is for our Bulk Activities collection.  This is intended only for adding 25+ contacts at a time using bulk jobs.  If you are looking for code for modifying or creating a single contact, please see this post for sample C# .NET code.  This code is easily modifyable to VB .NET and has been tested there as well.


 


Please note, it is against the Terms & Conditions of the Constant Contact API to use the Bulk Activity Collection for single contact adds such as Sign Up Forms.  If such abuse is detected, deactivation of the API Key may occur until the code is updated to not abuse the Constant Contact API.


 


 

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

View API documentation, code samples, get your API key.

Visit Page