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
... View more
I used some VB.Net code I found to create a page that successfully adds contacts to the database, however I am having difficulty with adding them to the correct list.
I can add them to the first list (general interest) without problem, but if I change the list number to 3 (our third list), then I get an invalid page request... Here's the lines I have for addint to the list:
Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/3"
data.Append("&lists=" + HttpUtility.UrlEncode(sListUri))
Like I said, it works fine if the number on the end of the first line is '1', but it doesn't if it is '3'
... View more