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.
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?