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("api_type", "json");
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);
wc.Dispose();
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);
JavaScriptSerializer deserializer = new JavaScriptSerializer();
Dictionary<string, object> dictJson = deserializer.Deserialize<Dictionary<string, object>>(stringJsonResponse);
The JavaScriptSerializer class can handle JSON, even though it doesn't sound like it by it's name. Be observant as it requires the using System.Web.Script.Serialization; statement, but it is deceiving It refers to a library contained in the System.Web.Extensions, so be sure to reference that in your project. Don't look in vain and try to reference something explicitly called System.Web.Script.Serialization.
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.
String password;
{
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!
- BlackOdd
What a terrific sounding book - the water cycle is cool in and of itself and a book which teaches it in such a fun poetic way is mega-cool.
ReplyDeleteCheap Essay Writing Services
Term Paper Writing
Accounts Software For Small Business