我尝试使用此网址将图片上传到pinterest
我一直在使用fiddler来复制所有必需的标题,但是当我运行我的代码时我收到了这个回复
错误=呃哦!我们遇到了这个问题。再试一次,或选择一个新图像。
这是fiddler使用firefox
请求的屏幕截图请求https://s22.postimg.cc/927sq5woh/success.png
syntaxview 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
syntaxview 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);
}
我不明白为什么我的代码不起作用,是不是因为我的图像字节数组?
答案 0 :(得分:0)
我找到了方法,没有将图像字节写入内存流是我的错,我从中学习link
并删除此行
Stream dataStream = request.GetRequestStream();
dataStream.Write(postData, 0, postData.Length);