Punkbastard

SeafightAPI - C#

Jul 1st, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace SeafightAPI
  8. {
  9.     public class SeafightAPI
  10.     {
  11.         private CookieAwareWebClient webclient = new CookieAwareWebClient();
  12.  
  13.         private string _server, _error;
  14.         private string _host = "http://www.seafight.bigpoint.com";
  15.  
  16.         public string Host
  17.         {
  18.             get { return this._host; }
  19.             set { this._host = value; }
  20.         }
  21.  
  22.         public string Server
  23.         {
  24.             get { return this._server; }
  25.             set { this._server = value; }
  26.         }
  27.  
  28.         public string Error
  29.         {
  30.             get { return this._error; }
  31.             set { this._error = value; }
  32.         }
  33.  
  34.         public bool Login(string username, string password)
  35.         {
  36.             try
  37.             {
  38.                 string sHome = String.Empty;
  39.                 using (webclient)
  40.                 {
  41.                     sHome = webclient.DownloadString(_host);
  42.                 }
  43.  
  44.                 if (String.IsNullOrEmpty(sHome))
  45.                 {
  46.                     this._error = "Could not load Seafight homepage";
  47.                     return false;
  48.                 }
  49.  
  50.                 string loginUri = "https://" + Regex.Match(sHome, @"<form name=""bgcdw_login_form"" method=""post"" class=""bgcdw_login_form"" action=""https\:\/\/(.*?)"">").Groups[1].Value.Replace("&amp;", "&");
  51.  
  52.                 if (loginUri.Equals("http://"))
  53.                 {
  54.                     this._error = "Could not find login uri";
  55.                     return false;
  56.                 }
  57.  
  58.                 string sLoginResult = String.Empty;
  59.                 using (webclient)
  60.                 {
  61.                     sLoginResult = webclient.UploadString(loginUri, "username=" + username + "&password=" + password);
  62.                 }
  63.  
  64.                 if (String.IsNullOrEmpty(sLoginResult))
  65.                 {
  66.                     this._error = "LoginResult Empty";
  67.                     return false;
  68.                 }
  69.  
  70.                 if (!sLoginResult.Contains("displayGold"))
  71.                 {
  72.                     this._error = "Incorrect username or password";
  73.                     return false;
  74.                 }
  75.  
  76.                 this._server = Regex.Match(sLoginResult, @".*</script><link rel=""meta"" href=""http\:\/\/(.*?)\.seafight\.bigpoint\.com\/sharedpages\/icra\/labels\.php"".*").Groups[1].Value;
  77.                
  78.                 return true;
  79.             }
  80.             catch (Exception ex)
  81.             {
  82.                 this._error = ex.Message;
  83.                 return false;
  84.             }
  85.         }
  86.  
  87.         public bool SendData(string data)
  88.         {
  89.             try
  90.             {
  91.                 string sMarket = String.Empty;
  92.                 using (webclient)
  93.                 {
  94.                     sMarket = webclient.DownloadString("http://" + this._server + ".seafight.bigpoint.com/index.es?action=internalMarketplace&subact=Elite");
  95.                 }
  96.  
  97.                 if (String.IsNullOrEmpty(sMarket))
  98.                 {
  99.                     this._error = "Could not load marketplace";
  100.                     return false;
  101.                 }
  102.  
  103.                 string token = Regex.Match(sMarket, "<input type=\"hidden\" name=\"reloadToken\" value=\"(.*?)\" />").Groups[1].Value;
  104.                 string rtvt = Regex.Match(sMarket, "<input type='hidden' name='RTVT' value='(.*?)'>").Groups[1].Value;
  105.  
  106.                 if (String.IsNullOrEmpty(token) || String.IsNullOrEmpty(rtvt))
  107.                 {
  108.                     this._error = "ReloadToken and/or RTVT found could not be extracted";
  109.                     return false;
  110.                 }
  111.  
  112.                 using (webclient)
  113.                 {
  114.                     webclient.UploadString("http://" + this._server + ".seafight.bigpoint.com/ajax.es", data + "&reloadToken=" + token + "&RTVT=" + rtvt);
  115.                 }
  116.  
  117.                 return true;
  118.             }
  119.             catch (Exception ex)
  120.             {
  121.                 this._error = ex.Message;
  122.                 return false;
  123.             }
  124.         }
  125.  
  126.         public string DownloadPage(Uri page)
  127.         {
  128.             try
  129.             {
  130.                 string result = String.Empty;
  131.                 using (webclient)
  132.                 {
  133.                     result = webclient.DownloadString(page);
  134.                 }
  135.  
  136.                 if (String.IsNullOrEmpty(result))
  137.                 {
  138.                     return "No page could be downloaded.";
  139.                 }
  140.  
  141.                 return result;
  142.             }
  143.             catch (Exception ex)
  144.             {
  145.                 return ex.Message;
  146.             }
  147.         }
  148.     }
  149.  
  150.     // The CookieAwareWebClient we use
  151.     // Source: http://pastebin.com/nGMNAJPG
  152.     internal class CookieAwareWebClient : WebClient
  153.     {
  154.         private readonly CookieContainer m_container = new CookieContainer();
  155.  
  156.         protected override WebRequest GetWebRequest(Uri address)
  157.         {
  158.             WebRequest request = base.GetWebRequest(address);
  159.             HttpWebRequest webRequest = request as HttpWebRequest;
  160.             if (webRequest != null)
  161.             {
  162.                 webRequest.CookieContainer = m_container;
  163.             }
  164.             return request;
  165.         }
  166.  
  167.         protected override WebResponse GetWebResponse(WebRequest request)
  168.         {
  169.             WebResponse response = base.GetWebResponse(request);
  170.             String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
  171.  
  172.             if (setCookieHeader != null)
  173.             {
  174.                 this.m_container.SetCookies(response.ResponseUri, setCookieHeader);
  175.             }
  176.             return response;
  177.         }
  178.     }
  179. }
Add Comment
Please, Sign In to add comment