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.

Adding contacts to the activities

Highlighted
Occasional Participant

Adding contacts to the activities

Hi Guys,


I'm adding contacts to the lists thru activities. I'm getting The remote server returned an error: (400) Bad Request. Code is as follows


 


  HttpWebRequest oRequest = null;


            oRequest = (HttpWebRequest)LoginCredReq("https://api.constantcontact.com/ws/customers/username/activities");

            oRequest.ContentType = "multipart/form-data; boundary=" + PostData.boundary;

            oRequest.Method = "POST";

            PostData pData = new PostData();

            Encoding encoding = Encoding.UTF8;

            Stream oStream = null;




            pData.Params.Add(new PostDataParam("activityType", "SV_ADD", PostDataParamType.Field));

            pData.Params.Add(new PostDataParam("lists", "http://api.constantcontact.com/ws/customers/username/lists/1", PostDataParamType.Field));

            pData.Params.Add(new PostDataParam("data",  "C:\\test.xls","file", PostDataParamType.File));




            byte[] buffer = encoding.GetBytes(pData.GetPostData());


            oRequest.ContentLength = buffer.Length;


            oStream = oRequest.GetRequestStream();

            oStream.Write(buffer, 0, buffer.Length);

            oStream.Close();


            HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();


 


 


 


private WebRequest LoginCredReq(string sUri)

    {

        CredentialCache LoginCredentials = new CredentialCache();

        LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/username"), "Basic",

        new NetworkCredential("apikey%username", "password"));

        // Create WebRequest, this is a factory class with a static constructor

        // You can not use new, all requests must be created with WebRequest.Create

        WebRequest Request = WebRequest.Create(sUri); // URI for the GET

        // Set Request credentials

        Request.Credentials = LoginCredentials;

        return Request;

    }

 

 

---------------------------------------------------------------------------------------------------------

public class PostDataParam

{

    public PostDataParam(string name, string value, PostDataParamType type)

    {

        Name = name;

        Value = value;

        Type = type;

    }

 

    public PostDataParam(string name, string filename, string value, PostDataParamType type)

    {

        Name = name;

        Value = value;

        FileName = filename;

        Type = type;

    }

 

    public string Name;

    public string FileName;

    public string Value;

    public PostDataParamType Type;

}


 


/// <summary> /// Returns the parameters array formatted for multi-part/form data

 


/// </summary>

 


/// <returns></returns>

 


{


 


 


{


sb.AppendLine(


 


{


sb.AppendLine(


sb.AppendLine(


sb.AppendLine();


sb.AppendLine(p.Value);


}


 


public string GetPostData()StringBuilder sb = new StringBuilder();foreach (PostDataParam p in m_Params)"--" + boundary);if (p.Type == PostDataParamType.File)string.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", p.Name, p.FileName));"Content-Type: application/octet-stream");else

{


sb.AppendLine(


sb.AppendLine();


sb.AppendLine(p.Value);


}


}


sb.AppendLine(


 


}


}


string.Format("Content-Disposition: form-data; name=\"{0}\"", p.Name));"--" + boundary + "--");return sb.ToString();

 


 



 



 

2 REPLIES 2
Highlighted
Employee

Adding contacts to the activities

The only issue I can see by looking at this is that you are using an excel file which doesn't work properly in C#. I would attempt to upload a .csv file with this and see if this has better results. If not, if you want to zip your project (or classes) up and send it over to our webservices support team, I would be happy to take a closer look at this. Thanks.

David J

Highlighted
Occasional Participant

Thanks so much

Thanks so much. I'll try to upload the csv file and let you know result.


 

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