模拟浏览器图像上传到imgur

时间:2014-12-24 14:34:48

标签: c# http httpwebrequest imgur

我在浏览器(Firefox Dev.Ed.)和imgur之间嗅探了图片上传过程中的流量,因为我希望能够使用官方API上传不带的图片。 您可以通过我的保管箱链接下载流量捕获: https://www.dropbox.com/s/6xfh6mdf03fe4dz/imgur_sniff.pcapng?dl=0

这是我解释捕获的方式:
首先,每次上传都会成为Upload-Session ID,通过向http://imgur.com/upload/start_session发送请求来接收。来自此连接的传入响应包含包含此ID的标头属性Set-Cookie。 在此之后(Wireshark-sniff中的新TCP-Stream),调用http://imgur.com/upload/checkcaptcha?total_uploads=1,其中total_uploadsGET - 属性,可能表示将要上载的图像数量。

以下是上述请求的回复:

5d
...........VJI,IT..V./K-....,)V.2.Q*-..OL.O./.+Q.22..Q*.MNN-.J......%.%.@...A-.......p_..G...
0

我不知道这意味着什么,如果这代表一个字符串? 下一步是请求http://imgur.com/crossdomain.xml,但我认为没有必要,因为只发送了冗余信息。

下一个TCP-Stream可能是最有趣的一个: 通过POST(Multipart-FormData)调用http://imgur.com/upload, 最后一个bodypart是OCTET - 应该上传的图像流。 响应包含图像的链接。

现在这是我的实施:

// Getting the Upload-Session
HttpWebRequest sessionRequest = (HttpWebRequest) WebRequest.Create("http://imgur.com/upload/start_session");
sessionRequest.Host = "imgur.com";
sessionRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0";
sessionRequest.Accept = "application/json, text/javascript, */*; q=0.01";
sessionRequest.Headers.Add("Accept-Language", "de,en-US;q=0.7,en;q=0.3");
sessionRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
sessionRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
sessionRequest.Referer = "http://imgur.com/";
sessionRequest.KeepAlive = true;
sessionRequest.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse) sessionRequest.GetResponse();
string imgurSession = response.Headers.Get("Set-Cookie");

MultiPartFormData formData = new MultiPartFormData();
formData.Add("Filename", imageFile.Name);
formData.Add("forceAnnonymous", "false");
formData.Add("total_uploads", "1");
formData.Add("sid", imgurSession.Split(';')[0].Split('=')[1]);
formData.Add("catify", "0");
formData.Add("current_upload", "1");
formData.Add("create_album", "0");
formData.Add("layout", "b");
formData.Add("gallery_submit", false.ToString());
formData.Add("album_title", "Optional Album Title");
formData.Add("edit", "0");
formData.AddCustomEntry("Content-Disposition: form-data; name=\"Filedata\"; filename=\"" +
                        imageFile.Name + "\"" + Environment.NewLine + "Content-Type: application/octet-stream" + Environment.NewLine
                        + Environment.NewLine);
FileStream fileStream = new FileStream(imageFile.FullName, FileMode.Open);
byte[] fileBuffer = new byte[imageFile.Length];
byte[] lastFormData = Encoding.UTF8.GetBytes(formData.CreateEntry("Upload", "Sumbit Query" + Environment.NewLine +
                                                                    "------------" + formData.Ticks + "--"));

HttpWebRequest uploadRequest = (HttpWebRequest) WebRequest.Create("http://imgur.com/upload");
uploadRequest.Method = "POST";
uploadRequest.Accept = "text/*";
uploadRequest.ContentType = "multipart/form-data; boundary=----------" + formData.Ticks;
uploadRequest.UserAgent = "Shockwave Flash";
uploadRequest.Host = "imgur.com";
uploadRequest.ContentLength = fileBuffer.Length + formData.Length + lastFormData.Length;
uploadRequest.KeepAlive = true;
uploadRequest.Headers.Add("Cache-Control", "no-cache");

Stream uploadStream = uploadRequest.GetRequestStream();
uploadStream.Write(formData.ToBytes(), 0, formData.Length);
while (fileStream.Read(fileBuffer, 0, fileBuffer.Length) != 0)
{
    uploadStream.Write(fileBuffer, 0, fileBuffer.Length);
}
uploadStream.Write(lastFormData, 0, lastFormData.Length);
StreamReader reader = new StreamReader(uploadRequest.GetResponse().GetResponseStream());

你怎么看我访问http://imgur.com/upload/checkcaptcha ?,因为我没有看到任何新信息,我将不得不执行大量请求,你怎么看到你打开Wireshark中相应的TCP-Stream。 我接受的回应如下:

  

达   {"数据" {"错误" {"代码":1003,"消息":" [2-溴丁] :文件类型无效> (应用/八位字节流)""类型":" Exception_UploadError""例外":{}},"请求&#34 ;:" /上传""方法":" POST"}"成功":假,"状态" :500}   0

我真的非常谨慎地重建最后一次上传请求,以便Wireshark告诉我,我对http://imgur.com/upload的请求与浏览器发送的请求完全相同。不过,我收到了一个错误。好的,还是有一些区别: 浏览器在标题中使用Cookie-Attribute发送请求,如下所示:

__cfduid=df2407e2f959c4382aaf466c62265995a1418721431; __qca=P0-647698388-1419247516502; o=0.40444; m_sort=viral; m_window=day; m_section=hot; OX_plg=swf|shk|pm; __gads=ID=3bfd01a8b049d7c8:T=1419248272:S=ALNI_MZiowWAL9UL03fWOwJTV58emtv6xQ; IMGURSESSION=o8dkrlfmiufhtvga55898eceb4; _nc=1; UPSERVERID=i-362e5bdc; _ga=GA1.2.761672825.1419247517; __utmt=1; optimizelyEndUserId=oeu1419355284536r0.03670886438339949; __utma=247341212.761672825.1419247517.1419255358.1419355284.3; __utmb=247341212.3.10.1419355284; __utmc=247341212; __utmz=247341212.1419247517.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); optimizelySegments=%7B%221503675771%22%3A%22gc%22%2C%221505515733%22%3A%22false%22%2C%221506465808%22%3A%22direct%22%2C%221717910483%22%3A%22none%22%2C%221893480508%22%3A%22true%22%2C%221938330135%22%3A%22true%22%2C%222025850214%22%3A%22true%22%2C%222359290357%22%3A%22true%22%7D; optimizelyBuckets=%7B%7D

虽然我只在Multipart-FormdData上传中发送了Session-ID作为bodypart(浏览器上传也是如此)。 您认为这次失败的原因是什么?我该如何解决?

注意:Wireshark-sniff不包含对http://imgur.com/upload/start_session的第一个请求,我不知道为什么没有被捕获,但我描述的TCP-Stream已经足够了。

1 个答案:

答案 0 :(得分:0)

您正尝试上传到http://imgur.com/upload/start_session

检查http://imgur.com/robots.txt会产生这个:

# robots

User-agent: *
Disallow: /account/
Disallow: /delete/
Disallow: /download/
Disallow: /logout/
Disallow: /removalrequest/
Disallow: /upload/
Disallow: /1/
Disallow: /2/
Disallow: /3/
Sitemap: /gallery/sitemap.xml

他们非常明确地没有自动化对/ upload /的请求。所以你不应该这样做。此外,“Mozilla / 5.0(Windows NT 6.3; WOW64; rv:36.0)Gecko / 20100101 Firefox / 36.0”是一个firefox用户代理,而不是脚本的使用者。说谎你的身份真的很糟糕。网站维护者提供了远离您的指示,您应该学会尊重您尝试访问的网站所有者施加的条件。

现在停止并使用官方API。你在这里尝试做什么都没有借口。