Hi David,
This was working fine few days ago. I added about 50-60 contacts in my list. I tried this 4-5 times and it worked. But today when i tried to run this, I was getting an exception. FYI - I have been using trial version (60 days free) account on constant and contact. Not sure if that would have caused the problem. Here is my code.
public void Bulk_POST(string APIKey, string uName, string pWord)
{
SqlConnection scon = new SqlConnection(@"connection string goes here");
SqlCommand scmd = new SqlCommand("SELECT * FROM Table WHERE LEN() > 0", scon);
SqlDataReader sdr;
try
{
scon.Open();
sdr = scmd.ExecuteReader();
while (sdr.Read())
{
CredentialCache LoginCredentials = new CredentialCache();
LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" +
uName + "/activities");
request.Credentials = LoginCredentials;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
sListUri = "https://api.constantcontact.com/ws/customers/" + uName + "/lists/3";
StringBuilder data = new StringBuilder();
data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8));
data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,First Name,Last Name,Company Name,CUSTOM FIELD 1" + "\n"), Encoding.UTF8));
data.Append(HttpUtility.UrlEncode((sdr.ToString() + "," + sdr.ToString() + "," + sdr.ToString() + "," + "SA" + "," + sdr.ToString()), Encoding.UTF8));
data.Append("&lists=" + HttpUtility.UrlEncode(sListUri));
byte[] byteArray = Encoding.UTF8.GetBytes(data.ToString());
try
{
request.ContentLength = byteArray.Length;
string XMLResponse = "Bytes to send: " + byteArray.Length;
Stream streamRequest = request.GetRequestStream();
streamRequest.Write(byteArray, 0, byteArray.Length);
streamRequest.Close();
HttpWebResponse Response = (HttpWebResponse)request.GetResponse();
//THE EXCEPTION IS THROWN IN THE LINE BELOW.
StreamReader Reader = new StreamReader(Response.GetResponseStream());
XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();
Reader.Close();
Response.Close();
//return XMLResponse;
}
catch (WebException e)
{
throw new WebException ("WebException: " + e.Status + "With response: " + e.Message);
}
catch (Exception e)
{
throw new Exception ("Exception: " + e.Message);
}
}
sdr.Close();
}
catch (Exception ex)
{
throw new Exception("Error occured: " + ex);
}
finally
{
scon.Close();
}
}
This same code was working fine like couple days ago. Please let me know if you want any further information.
Thanks!
... View more