patterncsharpMinor
Reddit account creator
Viewed 0 times
creatorredditaccount
Problem
I'm trying to make a program that will create an account on Reddit using an
I followed a tutorial on YouTube that poorly explained how everything about a
Currently, the program will create a Reddit account as long as the username hasn't been taken already. I just don't want any excess code that is useless. I want any extra code removed that isn't contributing to my goal mentioned above.
```
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System;
namespace RedditAccountCreate
{
public partial class RedditAccountCreate : Form
{
public RedditAccountCreate()
{
InitializeComponent();
}
CookieContainer CreateAccountCookie;
private void CreateAccount_Button_Click(object sender, EventArgs e)
{
string POSTData = "op=reg&dest=https%3A%2F%2Fwww.reddit.com%2F&user=" + Username_TextBox.Text + "&passwd=" + Password_TextBox.Text + "&passwd2=" + Password_TextBox.Text + "&email=&api_type=json";
CookieContainer TemporaryCookie = new CookieContainer();
UTF8Encoding Encoding = new UTF8Encoding();
byte[] ByteData = Encoding.GetBytes(POSTData);
HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("https://www.reddit.com/api/register/" + Username_TextBox.Text);
POSTRequest.Method = "POST";
POSTRequest.KeepAlive = true;
POSTRequest.CookieContainer = TemporaryCookie;
POSTRequest.ContentType = "application/x-www-form-urlencoded";
POSTRequest.Referer = "http://www.reddit.com/";
POSTRequest.UserAgent = "Mozi
HttpWebRequest. I ONLY want it to do that. (I don't care about it storing a cookie or anything else.) I just want it to be a simple program where you type a username into Username_TextBox.Text and a password into Password_TextBox.Text and then click the CreateAccount_Button and voilà, new account created!I followed a tutorial on YouTube that poorly explained how everything about a
HttpWebRequest worked.Currently, the program will create a Reddit account as long as the username hasn't been taken already. I just don't want any excess code that is useless. I want any extra code removed that isn't contributing to my goal mentioned above.
```
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System;
namespace RedditAccountCreate
{
public partial class RedditAccountCreate : Form
{
public RedditAccountCreate()
{
InitializeComponent();
}
CookieContainer CreateAccountCookie;
private void CreateAccount_Button_Click(object sender, EventArgs e)
{
string POSTData = "op=reg&dest=https%3A%2F%2Fwww.reddit.com%2F&user=" + Username_TextBox.Text + "&passwd=" + Password_TextBox.Text + "&passwd2=" + Password_TextBox.Text + "&email=&api_type=json";
CookieContainer TemporaryCookie = new CookieContainer();
UTF8Encoding Encoding = new UTF8Encoding();
byte[] ByteData = Encoding.GetBytes(POSTData);
HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("https://www.reddit.com/api/register/" + Username_TextBox.Text);
POSTRequest.Method = "POST";
POSTRequest.KeepAlive = true;
POSTRequest.CookieContainer = TemporaryCookie;
POSTRequest.ContentType = "application/x-www-form-urlencoded";
POSTRequest.Referer = "http://www.reddit.com/";
POSTRequest.UserAgent = "Mozi
Solution
Some points about your code
-
extract the code to create an account to a separate class. In this way you can reuse it without problems. This can even be a static method.
-
variables should be named using
-
as you don't use neither the
-
the assignment of
-
-
because
-
you don't use the
Implementing the mentioned points will lead to:
-
extract the code to create an account to a separate class. In this way you can reuse it without problems. This can even be a static method.
-
variables should be named using
camelCase casing. See the naming guidelines. -
as you don't use neither the
TemporaryCookie nor the CreateAccountCookie you should remove these varaibles. -
the assignment of
POSTRequest.KeepAlive = true; is superflous because the default of the KeepAlive property is true.-
string POSTRequestCode = POSTRequestReader.ReadToEnd(); is superflous because it is never used. -
because
Stream is implementing IDisposable you should enclose it in a using statement. This will take care that the Stream is closed and disposed.-
you don't use the
HttpWebResponse POSTResponse so you can remove it too Implementing the mentioned points will lead to:
private static string postDataToFormat = "op=reg&dest=https%3A%2F%2Fwww.reddit.com%2F&user={0}&passwd={1}&passwd2={1}&email=&api_type=json";
public static void CreateRedditAccount(string userName, string passWord)
{
string postData = string.Format(postDataToFormat, userName, passWord);
UTF8Encoding Encoding = new UTF8Encoding();
byte[] postValue = Encoding.GetBytes(postData);
HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("https://www.reddit.com/api/register/" + userName);
POSTRequest.Method = System.Net.WebRequestMethods.Http.Post;
POSTRequest.ContentType = "application/x-www-form-urlencoded";
POSTRequest.Referer = "http://www.reddit.com/";
POSTRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
POSTRequest.ContentLength = postValue.Length;
using (Stream POSTRequestStream = POSTRequest.GetRequestStream())
{
POSTRequestStream.Write(postValue, 0, postValue.Length);
}
}Code Snippets
private static string postDataToFormat = "op=reg&dest=https%3A%2F%2Fwww.reddit.com%2F&user={0}&passwd={1}&passwd2={1}&email=&api_type=json";
public static void CreateRedditAccount(string userName, string passWord)
{
string postData = string.Format(postDataToFormat, userName, passWord);
UTF8Encoding Encoding = new UTF8Encoding();
byte[] postValue = Encoding.GetBytes(postData);
HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("https://www.reddit.com/api/register/" + userName);
POSTRequest.Method = System.Net.WebRequestMethods.Http.Post;
POSTRequest.ContentType = "application/x-www-form-urlencoded";
POSTRequest.Referer = "http://www.reddit.com/";
POSTRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
POSTRequest.ContentLength = postValue.Length;
using (Stream POSTRequestStream = POSTRequest.GetRequestStream())
{
POSTRequestStream.Write(postValue, 0, postValue.Length);
}
}Context
StackExchange Code Review Q#100668, answer score: 3
Revisions (0)
No revisions yet.