3
\$\begingroup\$

I'm trying to make a program that will create an account on Reddit using an 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 = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
            POSTRequest.ContentLength = ByteData.Length;

            Stream POSTRequestStream = POSTRequest.GetRequestStream();
            POSTRequestStream.Write(ByteData, 0, ByteData.Length);
            POSTRequestStream.Close();
            HttpWebResponse POSTResponse = default(HttpWebResponse);

            POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();
            TemporaryCookie.Add(POSTResponse.Cookies);
            CreateAccountCookie = TemporaryCookie;
            StreamReader POSTRequestReader = new StreamReader(POSTResponse.GetResponseStream());

            string POSTRequestCode = POSTRequestReader.ReadToEnd();
        }

    }
}

Here's the link to the YouTube tutorial that I followed, in case anyone wants it.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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 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);
    }

}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.