C#使用HttpWebRequest将图像上载到pinterest

C#使用HttpWebRequest将图像上载到pinterest,c#,.net,httpwebrequest,pinterest,C#,.net,Httpwebrequest,Pinterest,我正在尝试使用此url将图像上载到pinterest 我一直在使用fiddler复制所有必需的标题,但我在运行代码时收到了此响应 错误=哦!我们在这件事上遇到了麻烦。请重试,或选择一个新图像 这是firefox中fiddler请求的屏幕截图 请求 syntaxview 多部分/表单数据 这是我的C#code的截图 byte[] ImgBytes = File.ReadAllBytes(targetfile); string boundary =

我正在尝试使用此url将图像上载到pinterest

我一直在使用fiddler复制所有必需的标题,但我在运行代码时收到了此响应

错误=哦!我们在这件事上遇到了麻烦。请重试,或选择一个新图像

这是firefox中fiddler请求的屏幕截图

请求

syntaxview

多部分/表单数据

这是我的C#code的截图

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

            }
请求

syntaxview

多部分/表单数据

和我的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);

            }
byte[]ImgBytes=File.ReadAllBytes(targetfile);
字符串边界=Guid.NewGuid().ToString();
字符串头=string.Format(“--{0}”,边界);
string foot=string.Format(“--{0}--”,边界);
StringBuilder内容=新建StringBuilder();
内容。附录行(标题);
AppendLine(String.Format(“内容处置:表单数据;名称=\“img\”文件名=\“{0}\”,targetfile));
contents.AppendLine(“内容类型:image/jpeg”);
contents.AppendLine();
AppendLine(Encoding.Default.GetString(ImgBytes));
内容。附录行(英尺);
var encoding=ascienceoding.UTF8;
byte[]postData=encoding.GetBytes(contents.ToString());
字符串url=”https://www.pinterest.com/upload-image/";
字符串XcsrfToken=“”;
字符串Pinterest_session=“”;
字符串_auth=“”;
字符串_b=“”;
如果(从列表中选择SingleCookieFromList(“c_名称”、“csrftoken”、“csrftoken”)。计数>1)
{
int totalCookie=从列表中选择SingleCookieFromList(“c_名称”、“csrftoken”、“csrftoken”)。计数;
for(int c=0;c1)
{
int totalCookie=从列表中选择SingleCookie(“c_name”、“c_pinterest”、“c_pinterest”)。计数;
for(int c=0;c1)
{
int totalCookie=从列表中选择SingleCookieFromList(“c_name”)、“_b”和“_b”)。计数;
for(int c=0;c1)
{
int totalCookie=从列表中选择SingleCookieFromList(“c_name”、“_auth”、“_auth”)。计数;
for(int c=0;c