Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Text.RegularExpressions;
- namespace SeafightAPI
- {
- public class SeafightAPI
- {
- private CookieAwareWebClient webclient = new CookieAwareWebClient();
- private string _server, _error;
- private string _host = "http://www.seafight.bigpoint.com";
- public string Host
- {
- get { return this._host; }
- set { this._host = value; }
- }
- public string Server
- {
- get { return this._server; }
- set { this._server = value; }
- }
- public string Error
- {
- get { return this._error; }
- set { this._error = value; }
- }
- public bool Login(string username, string password)
- {
- try
- {
- string sHome = String.Empty;
- using (webclient)
- {
- sHome = webclient.DownloadString(_host);
- }
- if (String.IsNullOrEmpty(sHome))
- {
- this._error = "Could not load Seafight homepage";
- return false;
- }
- string loginUri = "https://" + Regex.Match(sHome, @"<form name=""bgcdw_login_form"" method=""post"" class=""bgcdw_login_form"" action=""https\:\/\/(.*?)"">").Groups[1].Value.Replace("&", "&");
- if (loginUri.Equals("http://"))
- {
- this._error = "Could not find login uri";
- return false;
- }
- string sLoginResult = String.Empty;
- using (webclient)
- {
- sLoginResult = webclient.UploadString(loginUri, "username=" + username + "&password=" + password);
- }
- if (String.IsNullOrEmpty(sLoginResult))
- {
- this._error = "LoginResult Empty";
- return false;
- }
- if (!sLoginResult.Contains("displayGold"))
- {
- this._error = "Incorrect username or password";
- return false;
- }
- this._server = Regex.Match(sLoginResult, @".*</script><link rel=""meta"" href=""http\:\/\/(.*?)\.seafight\.bigpoint\.com\/sharedpages\/icra\/labels\.php"".*").Groups[1].Value;
- return true;
- }
- catch (Exception ex)
- {
- this._error = ex.Message;
- return false;
- }
- }
- public bool SendData(string data)
- {
- try
- {
- string sMarket = String.Empty;
- using (webclient)
- {
- sMarket = webclient.DownloadString("http://" + this._server + ".seafight.bigpoint.com/index.es?action=internalMarketplace&subact=Elite");
- }
- if (String.IsNullOrEmpty(sMarket))
- {
- this._error = "Could not load marketplace";
- return false;
- }
- string token = Regex.Match(sMarket, "<input type=\"hidden\" name=\"reloadToken\" value=\"(.*?)\" />").Groups[1].Value;
- string rtvt = Regex.Match(sMarket, "<input type='hidden' name='RTVT' value='(.*?)'>").Groups[1].Value;
- if (String.IsNullOrEmpty(token) || String.IsNullOrEmpty(rtvt))
- {
- this._error = "ReloadToken and/or RTVT found could not be extracted";
- return false;
- }
- using (webclient)
- {
- webclient.UploadString("http://" + this._server + ".seafight.bigpoint.com/ajax.es", data + "&reloadToken=" + token + "&RTVT=" + rtvt);
- }
- return true;
- }
- catch (Exception ex)
- {
- this._error = ex.Message;
- return false;
- }
- }
- public string DownloadPage(Uri page)
- {
- try
- {
- string result = String.Empty;
- using (webclient)
- {
- result = webclient.DownloadString(page);
- }
- if (String.IsNullOrEmpty(result))
- {
- return "No page could be downloaded.";
- }
- return result;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- }
- // The CookieAwareWebClient we use
- // Source: http://pastebin.com/nGMNAJPG
- internal class CookieAwareWebClient : WebClient
- {
- private readonly CookieContainer m_container = new CookieContainer();
- protected override WebRequest GetWebRequest(Uri address)
- {
- WebRequest request = base.GetWebRequest(address);
- HttpWebRequest webRequest = request as HttpWebRequest;
- if (webRequest != null)
- {
- webRequest.CookieContainer = m_container;
- }
- return request;
- }
- protected override WebResponse GetWebResponse(WebRequest request)
- {
- WebResponse response = base.GetWebResponse(request);
- String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
- if (setCookieHeader != null)
- {
- this.m_container.SetCookies(response.ResponseUri, setCookieHeader);
- }
- return response;
- }
- }
- }
Add Comment
Please, Sign In to add comment