MpghSession is an high abstraction class able to do provide a few of functions able to show a few of informations of a session user.
Code:
using System;
using System.Text;
using System. IO;
using System.Net;
// by javalover from http://mpgh.net
/// <summary>
/// An abstraction class which provides a few of useful methods to manage the user with selected username and password in context of the MPGH forum.
/// </summary>
public class MpghSession
{
private string _username;
private string _password;
private CookieContainer cookies;
public MpghSession(string username, string password)
{
_username = username;
_password = password;
}
/// <summary>
/// Returns the page content from an HttpWebResponse.
/// </summary>
/// <returns>String containing the page content.</returns>
private string GetContentFromResponse(HttpWebResponse webResponse)
{
return new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
}
/// <summary>
/// Does a login of the selected username and password and indicates if the login was successfull or unsuccessfull.
/// </summary>
/// <returns>True if successfull, false if unsuccessfull.</returns>
public bool DoLogin()
{
string fields = string.Format("vb_login_username={0}&vb_login_password={1}&vb_login_password_hint=Password&securitytoken=guest&do=login&vb_login_md5password=&vb_login_md5password_utf=", _username, _password);
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(fields);
CookieContainer temp = new CookieContainer();
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://www.mpgh.net/forum/login.php?do=login");
hwr.Method = "POST";
hwr.KeepAlive = true;
hwr.ContentType = "application/x-www-form-urlencoded";
hwr.CookieContainer = temp;
hwr.ContentLength = data.Length;
Stream stream = hwr.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse hwresp = (HttpWebResponse)hwr.GetResponse();
temp.Add(hwresp.Cookies);
cookies = temp;
string content = GetContentFromResponse(hwresp);
if (conten*****ntains("Thank you for logging in, " + _username))
return true;
return false;
}
/// <summary>
/// Goes to the homepage using the saved cookies.
/// </summary>
/// <returns>String containing homepage content.</returns>
public string GoHomepage()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://mpgh.net");
req.CookieContainer = cookies;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
return GetContentFromResponse(res);
}
/// <summary>
/// Gets and returns the notifies number of the current logged in user.
/// </summary>
/// <returns>Positive number if valid, -1 if not valid.</returns>
public int GetNotifyNumber()
{
string htmlPartOne = "<span class=\"notifications-number\"><strong>";
string htmlPartTwo = "</strong></span>";
string content = GoHomepage();
if (conten*****ntains(htmlPartOne) && conten*****ntains(htmlPartTwo))
{
string htmlFirstPart = content.Split(new[] { htmlPartOne }, StringSplitOptions.None)[1];
string result = htmlFirstPart.Split(new[] { htmlPartTwo }, StringSplitOptions.None)[0];
int notifies = -1;
bool isNumber = int.TryParse(result, out notifies);
return notifies;
}
else
{
return -1;
}
}
}
I didn't add many features, but I made it as an example to let other users add more features on it. This class was designed to be used in my program for a personal use, which is supposed to check the notifications number in MPGH every 15 seconds in background. If the notification number was higher than 0, it shows me a slide form saying I have a new notify when I'm not browsing the forum.
This is part of my program, an example:
Code:
// frmLogin.cs
private void frmLogin_Load(object sender, EventArgs e)
{
txtPassword.UseSystemPasswordChar = true;
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtUsername.Text) && !string.IsNullOrWhiteSpace(txtPassword.Text))
{
MpghSession session = new MpghSession(txtUsername.Text, txtPassword.Text);
if (session.DoLogin())
{
frmMain mainForm = new frmMain();
mainForm.session = session;
mainForm.Show();
this.Hide();
}
else
{
DialogResult dialogResult = MessageBox.Show("Login not valid. Please, check your username and password.\nIf you are sure your username is correct, you are probably blocked from MPGH for too many login tries. If so, try waiting 15 minutes.", string.Empty, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
btnLogin.PerformClick();
}
}
}
and finally, an example of the main form:
Code:
// frmMain.cs
public MpghSession session { private get; set; }
private void frmMain_Load(object sender, EventArgs e)
{
int notifies = session.GetNotifyNumber();
if (notifies >= 0)
{
MessageBox.Show("Currently you have " + notifies + " notifies on MPGH.");
}
}
I'm not going to post an out of the box solution, try implementing it yourself.