Benjamin, Thanks for the response. I tried settings.Encoding = Encoding.UTF8; however apparently .NET ignores is as it treats strings as UTF-16 soooo..... I've done some more reading about the encoding and modified my code as seen below and I am now getting a different error (500 - Internal Server Error). The XML should now be encoded as UTF-8 at least. /***
* Load into Constant Contact
***/
string APIKey = "[API KEY]";
string Username = "[USERNAME]";
string Password = "[PASSWORD]";
CredentialCache ccLoginCredentials = new CredentialCache();
ccLoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/[USERNAME]"), "Basic", new NetworkCredential(APIKey + "%" + Username, Password));
WebRequest wRequest = WebRequest.Create("https://api.constantcontact.com/ws/customers/" + Username + "/contacts");
wRequest.Method = "POST";
wRequest.ContentType = "application/atom+xml";
wRequest.Credentials = ccLoginCredentials;
string XMLData;
MemoryStream mStream = new MemoryStream();
using (XmlTextWriter xWriter = new XmlTextWriter(mStream, Encoding.UTF8))
{
xWriter.Indentation = 4;
xWriter.IndentChar = ' ';
xWriter.Formatting = Formatting.Indented;
xWriter.WriteStartDocument();
xWriter.WriteStartElement("entry", "http://www.w3.org/2005/Atom");
xWriter.WriteStartElement("title");
xWriter.WriteAttributeString("type", "text");
xWriter.WriteEndElement();
xWriter.WriteStartElement("updated");
xWriter.WriteString("2011-06-27T14:21:06.407Z");
xWriter.WriteEndElement();
xWriter.WriteStartElement("author");
xWriter.WriteEndElement();
xWriter.WriteStartElement("id");
xWriter.WriteString("https://api.constantcontact.com/ws/customers/[USERNAME]/lists/[LIST ID]");
xWriter.WriteEndElement();
xWriter.WriteStartElement("summary");
xWriter.WriteAttributeString("type", "text");
xWriter.WriteString("Contact");
xWriter.WriteEndElement();
xWriter.WriteStartElement("content");
xWriter.WriteAttributeString("type", "application/vnd.ctct+xml");
xWriter.WriteStartElement("Contact", "https://ws.constantcontact.com/ns/1.0/");
xWriter.WriteStartElement("EmailAddress");
xWriter.WriteString("test@test.com");
xWriter.WriteEndElement();
xWriter.WriteStartElement("OptInSource");
xWriter.WriteString("ACTION_BY_CUSTOMER");
xWriter.WriteEndElement();
xWriter.WriteStartElement("ContactLists");
xWriter.WriteStartElement("ContactList");
xWriter.WriteAttributeString("id", "https://api.constantcontact.com/ws/customers/[USERNAME]/lists/[LIST ID]");
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.WriteEndDocument();
xWriter.Flush();
StreamReader bReader = new StreamReader(mStream, Encoding.UTF8, true);
mStream.Seek(0, SeekOrigin.Begin);
XMLData = bReader.ReadToEnd();
}
//Response.Write(XMLData);
byte[] byteArray = Encoding.UTF8.GetBytes(XMLData);
try
{
wRequest.ContentLength = byteArray.Length;
Stream streamRequest = wRequest.GetRequestStream();
streamRequest.Write(byteArray, 0, byteArray.Length);
streamRequest.Close();
HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();
StreamReader sReader = new StreamReader(wResponse.GetResponseStream());
string xResponse = sReader.ReadToEnd();
Response.Write(xResponse);
}
catch (WebException ex)
{
Response.Write("WebException: " + ex.Status + " With response: " + ex.Message);
}
catch (XmlException xmlex)
{
Response.Write(xmlex.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
} Basically rather than using a StringBuilder I opted to try a MemoryStream object which allowed me to get the desired encoding (UTF-8). Below is my XML request... <?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text" />
<updated>2011-06-27T14:21:06.407Z</updated>
<author />
<id>https://api.constantcontact.com/ws/customers/[USERNAME]/lists/[LIST ID]</id>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="https://ws.constantcontact.com/ns/1.0/">
<EmailAddress>test@test.com</EmailAddress>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<ContactLists>
<ContactList id="https://api.constantcontact.com/ws/customers/[USERNAME]/lists/[LIST ID]" />
</ContactLists>
</Contact>
</content>
</entry> Any ideas why I would be getting a 500 error now? Again, thanks so much for assisting.
... View more