/// <summary>
/// Gets the main url.
/// </summary>
/// <value>The main url.</value>
public string UrlMain {
get { return "http://www.cgwallpapers.com/"; }
}
/// <summary>
/// Gets the login url.
/// </summary>
/// <value>The login url.</value>
public string UrlLogin {
get { return "http://www.cgwallpapers.com/login.php"; }
}
/// <summary>
/// Gets the login query string format.
/// </summary>
/// <value>The login query string format.</value>
public string UrlLoginQueryFormat {
get { return "usuario={0}&contrasena={1}"; }
}
/// <summary>
/// Gets the headers for a Login POST request.
/// </summary>
/// <value>The headers for a Login POST request.</value>
public WebHeaderCollection RequestHeadersPostLogin {
get {
WebHeaderCollection headers
= new WebHeaderCollection
(); var _with1 = headers;
_with1.Add("Accept-Language", "en-us,en;q=0.5");
_with1.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
_with1.Add("Keep-Alive", "99999");
return headers;
}
}
/// <summary>
/// Determines whether the user is logged in the site.
/// </summary>
private bool isLogged;
/// <summary>
/// Gets the cookie container.
/// </summary>
/// <value>The cookie container.</value>
public CookieCollection CookieCollection {
get { return this.cookieCollection1; }
}
/// <summary>
/// The cookie container.
/// </summary>
private CookieCollection cookieCollection1;
/// <summary>
/// Defines the query data for a LoginPost request.
/// </summary>
private sealed class LoginQueryData
{
/// <summary>
/// Gets the Usuario field.
/// </summary>
/// <value>The Usuario field.</value>
public string Usuario { get; set; }
/// <summary>
/// Gets or sets the Conteasena field.
/// </summary>
/// <value>The Conteasena field.</value>
public string Contrasena { get; set; }
}
/// <summary>
/// Gets a formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.
/// </summary>
/// <param name="loginQueryData">The <see cref="LoginQueryData"/> object that contains the login query fields.</param>
/// <returns>A formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.</returns>
private string GetLoginQueryString(LoginQueryData loginQueryData)
{
return string.Format(this.UrlLoginQueryFormat, loginQueryData.Usuario, loginQueryData.Contrasena);
}
/// <summary>
/// Sets the cookie container.
/// </summary>
/// <param name="url">The url.</param>
/// <param name="cookieCollection">The cookie collection.</param>
/// <returns>CookieContainer.</returns>
private CookieContainer SetCookieContainer(string url, CookieCollection cookieCollection)
{
CookieContainer cookieContainer
= new CookieContainer
(); System.DateTime refDate = default(System.DateTime);
foreach (Cookie oldCookie in cookieCollection) {
if (!DateTime.TryParse(oldCookie.Value, refDate)) {
Cookie newCookie
= new Cookie
(); var _with2 = newCookie;
_with2.Name = oldCookie.Name;
_with2.Value = oldCookie.Value;
_with2
.Domain = new Uri
(url
).Host; _with2.Secure = false;
cookieContainer.Add(newCookie);
}
}
return cookieContainer;
}
/// <summary>
/// Converts cookie string to global cookie collection object.
/// </summary>
/// <param name="cookie">The cookie string.</param>
/// <param name="cookieCollection">The cookie collection.</param>
private void SaveCookies(string cookie, ref CookieCollection cookieCollection)
{
string[] cookieStrings = cookie.Trim.Replace("path=/,", string.Empty).Replace("path=/", string.Empty).Split({ ';' }, StringSplitOptions.RemoveEmptyEntries);
cookieCollection
= new CookieCollection
();
foreach (string cookieString in cookieStrings) {
if (!string.IsNullOrEmpty(cookieString.Trim)) {
cookieCollection
.Add(new Cookie
(name
: cookieString
.Trim.Split('=')(0), value
: cookieString
.Trim.Split('=')(1)));
}
}
}
/// <summary>
/// Convert cookie container object to global cookie collection object.
/// </summary>
/// <param name="cookieContainer">The cookie container.</param>
/// <param name="cookieCollection">The cookie collection.</param>
/// <param name="url">The url.</param>
private void SaveCookies(CookieContainer cookieContainer, ref CookieCollection cookieCollection, string url)
{
cookieCollection
= new CookieCollection
();
foreach (Cookie cookie
in cookieContainer
.GetCookies(new Uri
(url
))) { cookieCollection.Add(cookie);
}
}
/// <param name="url">The url.</param>
/// <param name="cookieCollection">The cookie collection.</param>
/// <returns><c>true</c> if successfull, <c>false</c> otherwise.</returns>
private bool GetMethod(string url, ref CookieCollection cookieCollection)
{
Debug.WriteLine("[+] GetMethod function started.");
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader sr = null;
bool result = false;
try {
Debug.WriteLine("[+] Attempting to perform a request with:");
Debug.WriteLine(string.Format("Method: {0}", "GET"));
Debug.WriteLine(string.Format("Headers: {0}", string.Join(Environment.NewLine, this.RequestHeadersPostLogin)));
request = (HttpWebRequest)HttpWebRequest.Create(url);
var _with3 = request;
_with3.Method = "GET";
_with3.Headers = this.RequestHeadersPostLogin;
_with3.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
_with3.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0";
_with3.AllowAutoRedirect = false;
_with3.KeepAlive = true;
Debug.WriteLine("[-] Request done.");
// Get the server response.
Debug.WriteLine("[+] Getting server response...");
response = (HttpWebResponse)request.GetResponse;
Debug.WriteLine("[-] Getting server response done.");
if (request.HaveResponse) {
// Save the cookie info.
Debug.WriteLine("[+] Saving cookies...");
this.SaveCookies(response.Headers("Set-Cookie"), ref cookieCollection);
Debug.WriteLine("[-] Saving cookies done.");
// Get the server response.
Debug.WriteLine("[+] Getting server response...");
response = (HttpWebResponse)request.GetResponse;
Debug.WriteLine("[-] Getting server response done.");
Debug.WriteLine("[+] Reading server response...");
sr
= new StreamReader
(response
.GetResponseStream); using (sr) {
// Read the response from the server, but we do not save it.
sr.ReadToEnd();
}
result = true;
Debug.WriteLine("[-] Reading server response done.");
// No response received from server.
} else {
throw new Exception
(string.Format("No response received from server with url: {0}", url
)); result = false;
}
} catch (Exception ex) {
throw;
result = false;
} finally {
if (sr != null) {
sr.Dispose();
}
if (response != null) {
response.Close();
}
}
Debug.WriteLine("[-] GetMethod function finished.");
Debug.WriteLine("[i] Returning result value...");
return result;
}
/// <param name="loginData">The login post data.</param>
/// <param name="cookieCollection">The cookie collection.</param>
/// <returns><c>true</c> if successfull, <c>false</c> otherwise.</returns>
private bool PostLoginMethod(LoginQueryData loginData, ref CookieCollection cookieCollection)
{
Debug.WriteLine("[+] PostLoginMethod function started.");
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamWriter sw = null;
int initialCookieCount = 0;
string postData = null;
bool result = false;
try {
Debug.WriteLine("[+] Attempting to perform a login request with:");
Debug.WriteLine(string.Format("Method: {0}", "POST"));
Debug.WriteLine(string.Format("Referer: {0}", this.UrlMain));
Debug.WriteLine(string.Format("Accept: {0}", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
Debug.WriteLine(string.Format("ContentType: {0}", "application/x-www-form-urlencoded"));
Debug.WriteLine(string.Format("Headers: {0}", string.Join(Environment.NewLine, this.RequestHeadersPostLogin)));
request = (HttpWebRequest)HttpWebRequest.Create(this.UrlLogin);
var _with4 = request;
_with4.Method = "POST";
_with4.Headers = this.RequestHeadersPostLogin;
_with4.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
_with4.ContentType = "application/x-www-form-urlencoded";
_with4.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0";
_with4.Referer = this.UrlMain;
Debug.WriteLine("[-] Request done.");
Debug.WriteLine("[+] Passing request cookie info...");
// Pass cookie info from the login page.
if (cookieCollection != null) {
request.CookieContainer = this.SetCookieContainer(this.UrlLogin, cookieCollection);
}
Debug.WriteLine("[-] Passing request cookie info done.");
// Set the post data.
Debug.WriteLine("[+] Setting post data with:");
Debug.WriteLine(string.Format("Usuario: {0}", loginData.Usuario));
Debug.WriteLine(string.Format("Contrasena: {0}", loginData.Contrasena));
postData = this.GetLoginQueryString(loginData);
sw
= new StreamWriter
(request
.GetRequestStream); using (sw) {
sw.Write(postData);
// Post the data to the server.
}
Debug.WriteLine("[-] Setting post data done.");
// Get the server response.
Debug.WriteLine("[+] Getting server response...");
initialCookieCount = request.CookieContainer.Count;
response = (HttpWebResponse)request.GetResponse;
Debug.WriteLine("[-] Getting server response done.");
// Login successful.
if (request.CookieContainer.Count > initialCookieCount) {
result = true;
// Login unsuccessful.
} else {
result = false;
}
Debug.WriteLine(string.Format("[i] Login response result is: {0}", result ? "Successful (True)" : "Unsuccessful (False)"));
// Save new login cookies.
if (result) {
Debug.WriteLine("[+] Saving new login cookies...");
this.SaveCookies(request.CookieContainer, ref cookieCollection, this.UrlMain);
Debug.WriteLine("[-] Saving new login cookies done.");
}
} catch (Exception ex) {
throw;
} finally {
if (sw != null) {
sw.Dispose();
}
if (response != null) {
response.Close();
}
}
Debug.WriteLine("[-] PostLoginMethod function finished.");
Debug.WriteLine("[i] Returning result value...");
this.isLogged = result;
return result;
}
/// <summary>
/// Determines whether the account can log in CGWallpapers site.
/// </summary>
/// <returns><c>true</c> if the account can log in CGWallpapers site, <c>false</c> otherwise.</returns>
public bool CheckLogin(string username, string password)
{
if (this.GetMethod(this.UrlMain, this.cookieCollection1)) {
LoginQueryData loginQueryData
= new LoginQueryData
{ Usuario = HttpUtility.UrlEncode(username),
Contrasena = HttpUtility.UrlEncode(password)
};
return this.PostLoginMethod(loginQueryData, this.cookieCollection1);
} else {
return false;
}
// Me.GetMethod
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================