.NET VB -- POST Contact (Creating a New Contact / Contact Collections)

Highlighted
New Member

.NET VB -- POST Contact (Creating a New Contact / Contact Collections)

Thought this code may be of use to others who want to POST a new contact from .NET (RE: http://developer.constantcontact.com/doc/contactsCollection) . I found this post: http://developer.constantcontact.com/node/112 most helpful (C#). Here's what worked for me (VB):

==================================

Imports System.Net
Imports System.IO

Dim sUsername As String =
Dim sPassword As String =
Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/contacts"

'If you have more than one list, change this number to whichever list you're targeting
Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/1"
Dim sAPIKey As String =

Public Sub AddNewContact(ByVal Email As String)

'NOTE: Email value is passed, in this example, from textbox
'// Note: Remember to add "using System.Net;"
'// Create LoginCreditials

'Note: The login for the API is literally + % +
Dim NC As New NetworkCredential
NC.UserName = sAPIKey & "%" & sUsername
NC.Password = sPassword

'// Create WebRequest
Dim reqRequest As HttpWebRequest = WebRequest.Create(sUri)
reqRequest.Credentials = NC
reqRequest.Method = "POST"
'Note: For POST of single subscriber, use ATOM – application/atom+xml
reqRequest.ContentType = "application/atom+xml"

'// Set up XML String for the POST
'// Long string with quotes, use an absolute string with literals
' NOTE: This is a POST to the Contacts Collection –
' see http://developer.constantcontact.com/doc/contactsCollection (Creating a New Contact)
' // Set up the XML Document, application dependant

Dim XMLData As String
XMLData = ' NOTE --- plug in XMLData string from attached file

'// Convert XMLData to byteArray for posting
Dim byteArray As Byte() = 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
reqRequest.ContentLength = byteArray.Length
Dim XMLResponse As String '= "Bytes to send: " + byteArray.Length


'// Create a stream for the POST Request
Dim streamRequest As Stream = reqRequest.GetRequestStream()

'// Write the data to the stream.
streamRequest.Write(byteArray, 0, byteArray.Length)

streamRequest.Close()

'// 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
'// Note: Remember to add "using System.IO"


Using reqResponse As HttpWebResponse = TryCast(reqRequest.GetResponse(), HttpWebResponse)

Dim reader As New StreamReader(reqResponse.GetResponseStream())

XMLResponse = reader.ReadToEnd()

'// Close Reader
reader.Close()

End Using

'// Close the response to free up the system resources
Response.Close()

' ' optional -- return response with function
'Return XMLResponse

Catch e As WebException

'// Get the web exception type and response code
' return "WebException: " + e.Status + " With response: " + e.Message

Catch e As Exception

'// Get the exception type
'return "Exception:" + e.Message
End Try

End Sub


=======================================

Note: The "XMLData" code can be found in attached file.
Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured