0

I am unsuccessfully trying to programmatically upload an image to ezphotoshare.com with C#. They are using Chevereto v1 as their base. Every time I attempt to upload I get "The remote server returned an error: (400) Bad Request.". Here are some of things I've tried:

int limit = 32766;
        StringBuilder sb = new StringBuilder();
        int loops = img.Length / limit;
        for (int i = 0; i <= loops; i++)
        {
            if (i < loops)
            {
                sb.Append(Uri.EscapeDataString(img.Substring(limit * i, limit)));
            }
            else
            {
                sb.Append(Uri.EscapeDataString(img.Substring(limit * i)));
            }
        }
        string url = "http://ezphotoshare.com/api/1/upload/?key=######&source=" + sb +
                     "&format=json";
        var uploadImageRequest = (HttpWebRequest)WebRequest.Create(url);
        uploadImageRequest.Method = "POST";

        var response = (HttpWebResponse)uploadImageRequest.GetResponse();

Here is something else I tried:

string img = ImageReturn(@"C:\avatar63New3.jpg");
using (WebClient client = new WebClient())
        {
            byte[] response3 = client.UploadValues("http://ezphotoshare.com/api/1/", new NameValueCollection()
            {
                { "key", "######" },{"format","txt"},{"action","upload"},{"source",img}
            });
            Console.WriteLine(XDocument.Load(new MemoryStream(response3)));
        }

private static string ImageReturn(string imageLocation)
    {
        using (Image image = Image.FromFile(imageLocation))
        {
            using (MemoryStream m = new MemoryStream())
            {
                //image.Save(m, image.RawFormat);
                image.Save(m, ImageFormat.Jpeg);
                byte[] imageBytes = m.ToArray();

                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
    }

I've tried many different variations of the code above with no luck. Pointing me in the right direction would be greatly appreciated. Thanks.

Roro
  • 311
  • 2
  • 20

1 Answers1

0

Try using HttpClient way

Simple example how to use HttpClient:

https://stackoverflow.com/a/39414248/2154577

Community
  • 1
  • 1
Vinicius Maciel
  • 101
  • 2
  • 12
  • Thanks, but this project is using the 4.5 framework and I do not seem to be able to use the new way without changing the framework. Any other suggestions? – Roro May 09 '17 at 19:46
  • I understand your issue, do you already tested the api, using postman for example? If yes, was succeeded? – Vinicius Maciel May 09 '17 at 20:01