CHPP Development/CSharp.NET OOP/: Difference between revisions

From Hattrick
Jump to navigationJump to search
Amiad21 (talk | contribs)
No edit summary
 
Amiad21 (talk | contribs)
mNo edit summary
 
Line 43: Line 43:
               + userName + "&readonlypassword=" + securityCode;
               + userName + "&readonlypassword=" + securityCode;
             HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
             HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
             loHttp.UserAgent = "HTDreamLeague/v1.0";
             loHttp.UserAgent = "XXXXXXXXXX/v1.0";
             loHttp.CookieContainer = new CookieContainer();
             loHttp.CookieContainer = new CookieContainer();
             if (m_CookiesCol != null && m_CookiesCol.Count > 0)
             if (m_CookiesCol != null && m_CookiesCol.Count > 0)
Line 69: Line 69:
         String lcUrl = RecommendedServer + "/common/default.asp?outputType=XML&actionType=logout";
         String lcUrl = RecommendedServer + "/common/default.asp?outputType=XML&actionType=logout";
         HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
         HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
         loHttp.UserAgent = "HTDreamLeague/v1.0";
         loHttp.UserAgent = "XXXXXXXXXX/v1.0";
         loHttp.CookieContainer = new CookieContainer();
         loHttp.CookieContainer = new CookieContainer();
         if (m_CookiesCol != null && m_CookiesCol.Count > 0)
         if (m_CookiesCol != null && m_CookiesCol.Count > 0)
Line 121: Line 121:
         Encoding enc = Encoding.GetEncoding(1252);
         Encoding enc = Encoding.GetEncoding(1252);
         matchReq = (HttpWebRequest)WebRequest.Create(link);
         matchReq = (HttpWebRequest)WebRequest.Create(link);
         matchReq.UserAgent = "HTDreamLeague/v1.0";
         matchReq.UserAgent = "XXXXXXXXXX/v1.0";
         matchReq.CookieContainer = new CookieContainer();
         matchReq.CookieContainer = new CookieContainer();
         matchReq.CookieContainer.Add(CookiesCol);
         matchReq.CookieContainer.Add(CookiesCol);

Latest revision as of 16:19, 12 February 2008

This code will help you to login / logoff to HT and get XML files in the form of DataTables


I created 2 classes:

1. HTMatch class (the 2nd class in this page) to get the "matchdetails.xml" file. (You can use the same format to create class for any other XML file)

2. Login class that contains a HTMatch class in it.


public class Login

{

Class members:

   private CookieCollection m_CookiesCol = new CookieCollection();
   private HTMatch m_match;
   private String RecommendedServer;

Constructor:

   public Login()
   {
      RecommendedServer = getRecommendedServer();
   }

Get the recomended server:

   public static String getRecommendedServer()
   {
       DataSet xmlDataSet = new DataSet();
       DataTable xmlDataTable = new DataTable();
       xmlDataSet.ReadXml("http://www.hattrick.org/common/menu.asp?outputType=XML");
       xmlDataTable = xmlDataSet.Tables[0];
       DataRow[] xmlDataRow = xmlDataTable.Select("", "", DataViewRowState.CurrentRows);
       return (xmlDataRow[0]["RecommendedURL"]).ToString();
   }

Login to Hattrick. notice call it with the proper parameters.

   public String signIn(String userName, String securityCode)
   {
       try
       {
           String lcUrl = RecommendedServer + "/common/default.asp?outputType=XML&actionType=login&loginType=CHPP&Loginname="
              + userName + "&readonlypassword=" + securityCode;
           HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
           loHttp.UserAgent = "XXXXXXXXXX/v1.0";
           loHttp.CookieContainer = new CookieContainer();
           if (m_CookiesCol != null && m_CookiesCol.Count > 0)
               loHttp.CookieContainer.Add(m_CookiesCol);
           HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
           m_CookiesCol = loWebResponse.Cookies;
           if (loWebResponse.Cookies.Count > 0)
               m_CookiesCol = loWebResponse.Cookies;
           Encoding enc = Encoding.GetEncoding(1252);
           StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
           String lcHtml = loResponseStream.ReadToEnd();
           loWebResponse.Close();
           loResponseStream.Close();
           return lcHtml;
       }
       catch (Exception e)
       {
           return e.Message;
       }
   }

Logoff from Hattrick:

   public void logoff()
   {
       String lcUrl = RecommendedServer + "/common/default.asp?outputType=XML&actionType=logout";
       HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
       loHttp.UserAgent = "XXXXXXXXXX/v1.0";
       loHttp.CookieContainer = new CookieContainer();
       if (m_CookiesCol != null && m_CookiesCol.Count > 0)
           loHttp.CookieContainer.Add(m_CookiesCol);
       HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
       m_CookiesCol = loWebResponse.Cookies;
   }


Functions for "matchdetails.xml" file:

Get a new file frome HT and save it in the HTMatch member:

   public Boolean getNewHTMatch(String gameID)
   {
       m_match = new HTMatch(gameID, RecommendedServer, m_CookiesCol);
       return m_match.containsMatch();
   }

Check if the member holds in it a valid match:

   public Boolean chkHTMathOK()
   {
       return m_match.containsMatch();
   }

Get the HattrickData table from th HTMatch member:

   public DataTable getHTMatchHattrickData()
   {
       return m_match.getMatchHattrickData();
   }

}


public class HTMatch

{

Class members:

   private DataSet m_xmlMatchDataSet;
   private Boolean m_containsMatch = false;

Constructor:

   public HTMatch(String gameID, String RecommendedServer, CookieCollection CookiesCol)
   {
       DataSet xmlDataSet = new DataSet();
       StreamReader matchResponseStream;
       HttpWebResponse matchRes;
       string link = RecommendedServer + "/common/chpp/chppxml.axd?file=matchdetails&version=1.7&matchID="
           + gameID + "&matchEvents=true";
       HttpWebRequest matchReq;
       Encoding enc = Encoding.GetEncoding(1252);
       matchReq = (HttpWebRequest)WebRequest.Create(link);
       matchReq.UserAgent = "XXXXXXXXXX/v1.0";
       matchReq.CookieContainer = new CookieContainer();
       matchReq.CookieContainer.Add(CookiesCol);
       matchRes = (HttpWebResponse)matchReq.GetResponse();
       CookiesCol.Add(matchRes.Cookies);
       matchResponseStream = new StreamReader(matchRes.GetResponseStream(), enc, true, 20000);
       xmlDataSet.ReadXml(matchResponseStream);
       m_xmlMatchDataSet = xmlDataSet;
       matchRes.Close();
       matchResponseStream.Close();
       if ((String)xmlDataSet.Tables[0].Rows[0]["FileName"] == "matchdetails.xml")
           m_containsMatch = true;
       else
           m_containsMatch = false;
   }

Check if the DataTable has a valid match in it:

   public Boolean containsMatch()
   {
       return m_containsMatch;
   }

Return the tables from the downloaded file:

   public DataTable getMatchHattrickData()
   {
       return m_xmlMatchDataSet.Tables["HattrickData"];
   }
   public DataTable getMatchData()
   {
       return m_xmlMatchDataSet.Tables["Match"];
   }
   public DataTable getMatchHomeTeamData()
   {
       return m_xmlMatchDataSet.Tables["HomeTeam"];
   }
   public DataTable getMatchAwayTeamData()
   {
       return m_xmlMatchDataSet.Tables["AwayTeam"];
   }
   public DataTable getMatchArenaData()
   {
       return m_xmlMatchDataSet.Tables["Arena"];
   }
   public DataTable getMatchScorersListData()
   {
       return m_xmlMatchDataSet.Tables["Goal"];
   }
   public DataTable getMatchBookingsListData()
   {
       return m_xmlMatchDataSet.Tables["Booking"];
   }
   public DataTable getMatchEventList()
   {
       return m_xmlMatchDataSet.Tables["Event"];
   }

}