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.

Help with Uploading an Image

New Member

Help with Uploading an Image


I'm trying to upload an image file using an HttpWebRequest and am having a difficult time.  Seems that no matter what I try, I am recieving a 400 Error (Bad Request). Could anyone possibly help me figure out where the request is not valid?  Here is the code I am using (C#).


        string st = "";

        string sUri = ("" + sUsername + "/library/folders/" + folderid + "/images");

        Uri address = new Uri(sUri);


        HttpWebRequest request = (WebRequest.Create(address)) as HttpWebRequest;

        request.Credentials = new NetworkCredential((sAPIKey + ("%" + sUsername)), sPassword);


        request.Method = "POST";

        request.ContentType = "multipart/form-data";

        byte[] filedata = null;

        using (BinaryReader reader = new BinaryReader(File.OpenRead("test.jpg")))


            filedata = reader.ReadBytes((int)reader.BaseStream.Length);



        byte[] tmpHash = new MD5CryptoServiceProvider().ComputeHash(filedata);

        StringBuilder sb = new StringBuilder(tmpHash.Length);

        for (int i = 0; i < tmpHash.Length; i++)




        StringBuilder data = new StringBuilder();

        data.AppendLine("Content-Disposition: form-data; Content-Type: application/atom+xml");

        data.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");

        data.AppendLine("<atom:entry xmlns:atom=\"\">");

        data.AppendLine(" <atom:content>");

        data.AppendLine("  <Image>");

        data.AppendLine("   <FileName>test.jpg</FileName>");

        data.AppendLine("   <MD5Hash>" + sb.ToString() + "</MD5Hash>");

        data.AppendLine("   <Description />");

        data.AppendLine("  </Image>");

        data.AppendLine(" </atom:content>");



        data.AppendLine("Content-Disposition: form-data; Content-Transfer-Encoding: binary; name=\"file\"; filename=\"test.jpg\"; Content-Type: image/jpg");

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

        request.ContentLength = byteArray.Length + filedata.Length;

        Stream RequestStream = request.GetRequestStream();

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

        RequestStream.Write(filedata, 0, filedata.Length);



            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            StreamReader reader2 = new StreamReader(response.GetResponseStream());

            st = reader2.ReadToEnd();



        catch (Exception ex)


            st = ex.Message.ToString();


        return st;


Thanks for any help you can give me.





Hi Scott,   Good question on

Hi Scott,


Good question on this and it's something we're both looking to add to the C#.NET Wrapper Library and also put up a blog post on how to do (it's definitely more complicated than sending XML).  I did do a blog post on how to do this on our Labs website including a code sample for how to do this in a C# Form Application.  Code is resable in any other application or website, simply would need to include the 3 core .cs files into your application.  You can find the blog post and sample code here:

Dave Berard
Senior Product Manager, Constant Contact
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