I'm trying to get the VB.NET 'Add a Contact' example that's posted in a few different topics here to work, and I keep getting a (400) Bad Request response.
Here's my code... any help would be appreciated.
Dim sUsername As String = "myUserName"
Dim sPassword As String = "myPassword"
Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/activities"
Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/1"
Dim sAPIKey As String = "myAPIKey"
'Setup an HttpWebRequest
Dim address As New Uri(sUri)
Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
request.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
'Build data string
Dim data As New StringBuilder()
data.Append("activityType=" + HttpUtility.UrlEncode("ADD_CONTACTS", 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))
Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString())
Dim st As String = String.Empty
request.ContentLength = byteData.Length
Using postStream As Stream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
End Using
Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
st = reader.ReadToEnd()
End Using
... View more