This is a post directed to newcomers in .NET and C#, who wants to try their luck interacting with a website using JSON and some simple HTTP POST parameters.
First take a look at Reddits provided information
And here is some C# code to get you started. We'll go through most of the code in this post in a simplified manner.
Let's start with the WebClient class. It's useful for rudimentary interaction with websites, and can post to and recieve responses from a webserver. There is no graphical interaction however (for that you need the WebBrowser)
WebClient has the method UploadValues which is an easy way to make a POST command through the HTTP protocol. We will use this to post data to Reddit.
As we see here there are three parameters that needs to be sent to the URI of "http://www.reddit.com/api/login/USERNAME" where the USERNAME substring will be replaced by the name of the acount we are logging into.
These parameters are best stored in the NameValueCollection class, as the UploadValues method of WebClient needs this.
String userName = "YourUsername";
String password = "YourPassword";
NameValueCollection userdata = new NameValueCollection();
userdata.Add("user", userName );
userdata.Add("passwd", password );
Make a webclient and POST the data.
System.Net.ServicePointManager.Expect100Continue = false; //some servers choke without
var wc = new WebClient();
byte byteResponse = wc.UploadValues("http://www.reddit.com/api/login/"+ userName , "POST", userdata);
Due to how Reddit has configured their webserver, we need to set the global ServicePointManager.Expect100Continue property to false.
As a response from the server, we get a byte-array. In Reddits case, it's formated as JSON, a format for storing data. The JSON contains keys which are linked to values.
To make use of this data, we can serialize it into a Dictionary.
String stringJsonResponse = System.Text.Encoding.UTF8.GetString(byteJsonResponse);
Dictionary<string, object> dictJson = deserializer.Deserialize<Dictionary<string, object>>(stringJsonResponse);
You are now logged in, and you have received two keys inside the Dictionary called "modhash" and "cookie". These are important for further interaction with Reddit, and you can extract them in the following fashion.
var json = (Dictionary<string, object>)dictJson["json"];
var data = (Dictionary<string, object>)json["data"];
modhash = data["modhash"].ToString();
cookie = data["cookie"].ToString();
I will probably do a tutorial of how to vote on links next. I hope you learned something useful!