Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remotely downvote a Stack Overflow post with C#? [closed]

I might be trying to do something impossible or really hard, but I wanted to try it out anyway. I have been working on writing a program that can automatically downvote Stack Overflow posts for me.

So, my logical first step was to find out what went on behind the scenes when I pressed the downvote button. I used a HTTP network analyzer to see how the browser communicates to the server that I want to downvote. This is what it showed me.

Downvote details

Then I figured I should be able to remotely downvote it if I write a C# program that sends an HTTP request identical to the one I sent when I pressed the downvote button. So I came up with this:

WebRequest req = WebRequest.Create("http://stackoverflow.com/posts/3905734/vote/3");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 37;

req.Headers.Add("Request", "POST /posts/3905734/vote/3 HTTP/1.1");
req.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01");
req.Headers.Add("X-Requested-With", "XMLHttpRequest");
req.Headers.Add("Referer", "http://stackoverflow.com/questions/3905734/how-to-send-100-000-emails-weekly");
req.Headers.Add("Accept-Language", "en-us");
req.Headers.Add("Accept-Encoding", "gzip, deflate");
req.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)");
req.Headers.Add("Host", "stackoverflow.com");
req.Headers.Add("Connection", "Keep-Alive");
req.Headers.Add("Cache-Control", "no-cache");
req.Headers.Add("Cookie", "__utmc=140029553; __utma=140029553.1661295586.1330352934.1331336368.1331402208.44; __utmz=140029553.1331159433.33.7.utmcsr=meta.stackoverflow.com|utmccn=(referral)|utmcmd=referral|utmcct=/users/153008/cody-gray; __qca=P0-1737884911-1330352934366; usr=t=TJUTES9CakOu&s=f3MgHSwW2EWk; km_ai=91003; km_uq=; km_lv=x; km_ni=91003; __utmb=140029553.17.10.1331402208");

var requestMessage = Encoding.UTF8.GetBytes("fkey=abfd538253d7ca1e988f306ea992eda0");
var strm = req.GetRequestStream();
strm.Write(requestMessage, 0, requestMessage.Length);
strm.Close();

var rep = req.GetResponse();
strm = rep.GetResponseStream();
var rdr = new StreamReader(strm);
string responseFromServer = rdr.ReadToEnd();
Console.WriteLine(responseFromServer);
rdr.Close();
strm.Close();
Console.Read();

There were some headers that it would not let me write. For the headers Accept, Referer, User-Agent and Connection, it threw an error like this:

This header must be modified using the appropriate property or method.

and the Host header caused this error:

The 'Host' header cannot be modified directly.

I just commented out the headers that were causing trouble, optimistically hoping that it would still work anyway, but I got back this message from the server

{"Success":false,"Warning":false,"NewScore":0,"Message":"","Refresh":false}

The "Success":false seemed to indicate that it was not successful in downvoting the post, and I went to the page and it had the same vote count as it did before I ran the program. In other words, my program didn't work.

Does anybody know if I'm on the right path, what I can do to make it work, or if it's even possible to make it work?

like image 653
Peter Olson Avatar asked Mar 10 '12 18:03

Peter Olson


1 Answers

You might not consider this an answer, but I do; what you are trying to do is considered abuse of the system. We do not provide an API for that for many reasons, and trying to reverse engineer something that is not part of a published API is ... obnoxious.

Thanks for posting your security cookies. I will thus hit a button to log you out of SE, to protect your account from abuse. You will be able to log back in as normal.

We reserve the right to be frankly downright childish and puerile if you insist on trying to abuse the system; we're always happy to introduce extra hoops and hurdles, randomly change the API, etc if we believe you are trying to do nasty things. Or just suspend the account or block network access.

Allow me to summarise: we don't really want you doing this.

like image 166
Marc Gravell Avatar answered Sep 25 '22 19:09

Marc Gravell