-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCForgeAccount.cs
65 lines (57 loc) · 2.27 KB
/
MCForgeAccount.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace MCForge
{
public static class MCForgeAccount
{
public static bool LoggedIn { get; private set; }
public static CookieContainer Cookies { get; private set; }
public static bool Login()
{
return Login(Server.mcforgeUser, Server.mcforgePass);
}
public static bool Login(string user, string pass)
{
try
{
LoggedIn = false;
Server.s.Log("Logging into MCForge.net...");
byte[] data = Encoding.ASCII.GetBytes(string.Format("action=do_login&username={0}&password={1}", user, pass));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.mcforge.net/forums/member.php");
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.Timeout = 15000; // 15 seconds
using (Stream stream = request.GetRequestStream())
stream.Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string responseData = reader.ReadToEnd();
if (responseData.IndexOf("You have successfully been logged in.") != -1)
{
Server.s.Log("Successfully logged into MCForge.net!");
Cookies = new CookieContainer();
Cookies.Add(response.Cookies);
LoggedIn = true;
}
else Server.s.Log("Login failed!");
}
}
return LoggedIn;
}
catch (Exception ex)
{
Server.ErrorLog(ex);
return false;
}
}
}
}