首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#上传本地镜像到Chevereto接口v1 ezphotoshare.com

C#上传本地镜像到Chevereto接口v1 ezphotoshare.com
EN

Stack Overflow用户
提问于 2017-05-10 00:24:09
回答 1查看 182关注 0票数 0

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

代码语言:javascript
复制
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();

下面是我尝试过的其他方法:

代码语言:javascript
复制
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;
            }
        }
    }

我已经尝试了上面代码的许多不同的变体,但都没有成功。为我指明正确的方向将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-05-10 03:05:21

尝试使用HttpClient方式

简单示例如何使用HttpClient:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43875159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档