Hi, I am new to this site. I am in a pinch here and need to get this figured out quickly for a customer of mine. I am designing a simple site that will include a custom sign-up box for a Constant Contact newsletter. This requires that I use their API, which I have never had to do before. I found a post that included the necessary vb.net code, but I am having some problems with it. I am currently getting an error 400 (Bad request). The error is being thrown on line 33 but I am not sure that that is where my problem is. I have done some research on what the problem could be and it seems to be an error in the info I am sending to the server. That is why I included so much code. I would appreciate any help or direction anyone could offer. Thanks in advance.

URL: www.phonefrank.com

My Code for the section in question:

'Setup your variables
        Dim sUsername As String = "********"
        Dim sPassword As String = "********"
        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" 'If you have more than one list, change this number to whichever list you're targeting
        Dim sAPIKey As String = "684f5567-b3b9-455b-b266-a759a2d9ecb7"

        'Setup an HttpWebRequest to send the data
        Dim addresss As New Uri(sUri)
        Dim request As HttpWebRequest = TryCast(WebRequest.Create(addresss), HttpWebRequest)
        request.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"

        'Build an encoded string of the data to pass to Constant Contact
        'More info on this can be found at http://developer.constantcontact.com/doc/activities
        Dim data As New StringBuilder()
        data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8))
        data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,Name,Address,City,Zip Code" & Chr(10)), Encoding.UTF8))
        data.Append(HttpUtility.UrlEncode((TextBox5.Text & ",HTML," & TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text), Encoding.UTF8))
        data.Append("&lists=" + HttpUtility.UrlEncode(sListUri))

        'The "guts" of the code to execute the request and return a response
        'The response (returned as 'strResponse') will be XML.  You can parse this for status messages if you like, or just ignore it.
        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
    End Sub
8 Years
Discussion Span
Last Post by Ramy Mahrous
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.