C#将本地图像上传到Chevereto API v1 ezphotoshare.com

时间:2017-05-09 16:24:09

标签: c# asp.net base64 httpwebrequest webclient

我尝试使用C#以编程方式将图像上传到ezphotoshare.com,但未成功。他们使用Chevereto v1作为他们的基础。每次我尝试上传时都会收到“远程服务器返回错误:(400)错误请求。”。以下是我尝试过的一些事情:

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();

这是我尝试过的其他内容:

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;
            }
        }
    }

我尝试了上面代码的许多不同变体而没有运气。指出我正确的方向将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用HttpClient方式

如何使用HttpClient的简单示例:

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