C# 使用 HttpWebRequest 将图片上传到 pinterest

C# upload image to pinterest with HttpWebRequest

我正在尝试使用此 url

将图片上传到 pinterest

https://www.pinterest.com/upload-image/

我一直在使用 fiddler 来复制所有需要的东西 headers,但是当我 运行 我的代码

时收到了这个回复

error=Uh oh! We're having trouble with that one. Try again, or pick a new image.

这是使用 firefox 的 fiddler 请求的屏幕截图

请求https://s22.postimg.cc/927sq5woh/success.png

语法视图https://s22.postimg.cc/xl9u6z8n5/success2.png

multipart/form-data https://s22.postimg.cc/f5pd9lx3l/success3.png

这是我的 C# 代码的屏幕截图

请求https://s22.postimg.cc/vtgvceho1/failed.png

语法视图https://s22.postimg.cc/z0bew3ezl/failed2.png

multipart/form-data https://s22.postimg.cc/ktvo0w6pd/failed3.png

还有我的 C# 代码

            byte[] ImgBytes = File.ReadAllBytes(targetfile);
            string boundary = Guid.NewGuid().ToString();
            string head = string.Format("--{0}", boundary);
            string foot = string.Format("--{0}--", boundary);

            StringBuilder contents = new StringBuilder();
            contents.AppendLine(head);
            contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"img\"; filename=\"{0}\"", targetfile));
            contents.AppendLine("Content-Type: image/jpeg");
            contents.AppendLine();
            contents.AppendLine(Encoding.Default.GetString(ImgBytes));
            contents.AppendLine(foot);


            var encoding = ASCIIEncoding.UTF8;
            byte[] postData = encoding.GetBytes(contents.ToString());
            string url = "https://www.pinterest.com/upload-image/";

            string XcsrfToken = "";
            string Pinterest_session = "";
            string _auth = "";
            string _b = "";
            if (SelectSingleCookieFromList("c_name", "csrftoken", "csrftoken").Count > 1)
            {
                int totalCookie = SelectSingleCookieFromList("c_name", "csrftoken", "csrftoken").Count;
                for (int c = 0; c < totalCookie; c++)
                {
                    if (c == totalCookie - 1)
                    {
                        XcsrfToken = SelectSingleCookieFromList("c_name", "csrftoken", "csrftoken")[c];
                    }
                }
            }
            else
            {
                XcsrfToken = SelectSingleCookieFromList("c_name", "csrftoken", "csrftoken")[0];
            }
            if (SelectSingleCookieFromList("c_name", "_pinterest_sess", "_pinterest_sess").Count > 1)
            {
                int totalCookie = SelectSingleCookieFromList("c_name", "_pinterest_sess", "_pinterest_sess").Count;
                for (int c = 0; c < totalCookie; c++)
                {
                    if (c == totalCookie - 1)
                    {
                        Pinterest_session = SelectSingleCookieFromList("c_name", "_pinterest_sess", "_pinterest_sess")[c];
                    }
                }
            }
            else
            {
                Pinterest_session = SelectSingleCookieFromList("c_name", "_pinterest_sess", "_pinterest_sess")[0];
            }
            if (SelectSingleCookieFromList("c_name", "_b", "_b").Count > 1)
            {
                int totalCookie = SelectSingleCookieFromList("c_name", "_b", "_b").Count;
                for (int c = 0; c < totalCookie; c++)
                {
                    if (c == totalCookie - 1)
                    {
                        _b = SelectSingleCookieFromList("c_name", "_b", "_b")[c];
                    }
                }
            }
            else
            {
                _b = SelectSingleCookieFromList("c_name", "_b", "_b")[0];
            }
            if (SelectSingleCookieFromList("c_name", "_auth", "_auth").Count > 1)
            {
                int totalCookie = SelectSingleCookieFromList("c_name", "_auth", "_auth").Count;
                for (int c = 0; c < totalCookie; c++)
                {
                    if (c == totalCookie - 1)
                    {
                        _auth = SelectSingleCookieFromList("c_name", "_auth", "_auth")[c];
                    }
                }
            }
            else
            {
                _auth = SelectSingleCookieFromList("c_name", "_auth", "_auth")[0];
            }

            CookieContainer cookieJar = new CookieContainer();
            cookieJar.SetCookies(new Uri(url), "_auth=" + _auth + ", _b=" + _b.Replace("\"", "") + ", _pinterest_sess=" + Pinterest_session.Replace("\"", "") + ", csrftoken=" + XcsrfToken);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "POST";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.ContentLength = postData.Length;

            request.Accept = "application/json";
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("Accept-Language", "en-US");
            request.Headers.Add("DNT", "1");
            request.Host = "www.pinterest.com";
            request.Referer = "https://www.pinterest.com";
            request.Headers.Add("X-CSRFToken", XcsrfToken);
            request.Headers.Add("X-UPLOAD-SOURCE", "partner_uploader");
            request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36";
            request.CookieContainer = cookieJar;

            Stream dataStream = request.GetRequestStream();
            dataStream.Write(postData, 0, postData.Length);
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            WebHeaderCollection header = response.Headers; 
            using (var reader = new StreamReader(response.GetResponseStream(), encoding))
            {
                var responseText = JObject.Parse(reader.ReadToEnd());
                //Console.WriteLine(responseText);
                var status = (string)responseText["success"];
                var imgurl = (string)responseText["image_url"];
                Console.WriteLine(status + ":"+ imgurl);

            }

我不明白为什么我的代码不起作用,是因为我的图像字节数组吗?

我找到了方法,没有将图像字节写入内存流是我的错,我从中学习link

并删除这一行

Stream dataStream = request.GetRequestStream();
dataStream.Write(postData, 0, postData.Length);