The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Error 415 Unsupported media type - Adding multiple contacts to a contact list using C#

Highlighted
New Member

Error 415 Unsupported media type - Adding multiple contacts to a contact list using C#

I tried to add multiple contacts to my contact list using the example provided in the Bulk Activities. The code i use is given below. I get the Error "WebException: ProtocolErrorWith response: The remote server returned an error: (415) Unsupported Media Type." Can you please help me on how to add multiple contacts to a contact a list on a single invocation? Can you point out what is the error in the code given below? 


 public string Bulk_POST(string APIKey, string username, string password)


    {

        // Create login credentials

        CredentialCache LoginCredentials = new CredentialCache();

 

        // Create a new credential for this account

        LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/USERNAME" + username), "Basic", new NetworkCredential(APIKey + '%' + username, password));

 

        // Create a web request

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" + username + "/contacts");

 

        // Set Request to be a POST

        Request.Method = "POST";

 

        // Set the ContentType property.

        Request.ContentType = "application/x-www-form-urlencoded";

 

        // Set Request credentials

        Request.Credentials = LoginCredentials;

 

        // Set up XML String for the POST

        string XMLData = "activityType=ADD_CONTACTS"+

                         "&data=Email+Address%2CFirst+Name%2CLast+Name%0A"+

                         "wstest3%40example.com%2C+Fred%2C+Test%0A"+

                         "wstest4%40example.com%2C+Joan%2C+Test%0A"+

                         "wstest5%40example.com%2C+Ann%2C+Test"+                                       "&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fmyusername%2Flists%2F5"+

                 "&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fmyusername%2Flists%2F6";

 

        // Convert XMLData to byteArray for posting

        byte[] byteArray = Encoding.UTF8.GetBytes(XMLData);

 

        // Send POST request

        try

        {

            // Set the ContentLength portion of the header

            Request.ContentLength = byteArray.Length;

            string XMLResponse = "Bytes to send: " + byteArray.Length;

 

            // Create a stream for the POST Request // Note: Remember to add using System.IO

            Stream streamRequest = Request.GetRequestStream();

 

            // Write the data to the stream.

            streamRequest.Write(byteArray, 0, byteArray.Length);

            streamRequest.Close();

            HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

 

            // 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

 

            StreamReader Reader = new StreamReader(Response.GetResponseStream());

 

            // Read the entire XML response to a string

            // Note there may not be a XML response for a successful POST

            XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();

 

            // Close Reader

            Reader.Close();

 

            // Close the response to free up the system resources

            Response.Close();

 

            return XMLResponse;

        }

        catch (WebException e)

        {

            // Get the web exception type and response code

            return ("WebException: " + e.Status + "With response: " + e.Message);

        }

 

        catch (Exception e)

        {

            // Get the exception type

            return ("Exception: " + e.Message);

        }

 

    }

}

 

Thanks & Regards,

Kripa Vijay Anand

(kva888@live.com)

Kripa Vijay Anand


Software Developer 


Microsoft Dynamics CRM 4.0

1 REPLY 1
Highlighted
New Member

Error 415 Solved

hi guys,


 I identified the error in my code.  It is with the URI for creating a web request. I hv shown it below


 


Before:


// Create a web request

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" + username + "/contacts");


After:


// Create a web request

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" + username + "/activities");


 


Thanks for all your support

Kripa Vijay Anand


Software Developer 


Microsoft Dynamics CRM 4.0

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured