CHPP Development/C: Difference between revisions

From Hattrick
Jump to navigationJump to search
No edit summary
Amiad21 (talk | contribs)
No edit summary
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Step 1. Connecting to a recommended Server   
Step 1) Connecting to a recommended [[Server]]    


         static public String getRecommendedServer()
         static public String getRecommendedServer()
Line 13: Line 13:




Step 2. Sign-In
Step 2) Sign-In


       CookieCollection CookiesCol;
       CookieCollection CookiesCol;
Line 46: Line 46:
             }
             }
         }
         }
 
[[Category:Programming Languages|C]]
Step 3. Retrieve Data
 
        public static String downloadTeamDetails()
        {
            try
            {
                String downloadUrl = recommendedServerURL +
                          "/common/teamDetails.asp?outputType=XML&actionType=view";
                // Establish the request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(downloadUrl);
                // Set properties
                request.UserAgent = myUserAgent;
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cCollection[0]);
                // Retrieve request info headers
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Encoding enc = Encoding.GetEncoding(1252);
                StreamReader responseStream = new StreamReader(response.GetResponseStream(),enc);
                String outputTEMP = responseStream.ReadToEnd();
                //TO DO - here will eventually write to file!!
                response.Close();
                responseStream.Close();
                return outputTEMP;
            }
            catch (Exception e)
            {
                return e.StackTrace;
            }
        }
 
Step 4. Sign-Out (Logout)
 
        public static void signOut()
        {
            try
            {
                string closeUrl = recommendedServerURL + "/common/default.asp?outputType=XML&actionType=logout";
                // Establish the request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(closeUrl);
                // Set properties
                request.UserAgent = myUserAgent;
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cCollection[0]);
                // Retrieve request info headers
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                // Windows default Code Page
                Encoding enc = Encoding.GetEncoding(1252);
                //no need to open input stream, just connect
                response.Close();       
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }
        }
 
 
 
*Apologies for the messy code - but it does work, although it might need a little tidying up!! If you have any queries on any of this code please do not hesitate to contact me: dubbies @ Project Zero (on Hatrick) ;)

Latest revision as of 14:04, 11 February 2008

Step 1) Connecting to a recommended Server

       static public 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());
       }


Step 2) Sign-In

     CookieCollection CookiesCol;
       public String signIn()
       {
           try
           {
               String userName = "YOURUSERNAME";
               String securityCode = "YOURPASSWORD";
               String myUserAgent = "YOURAPPNAME/v1.0";
               string lcUrl = recommendedURL + "/common/default.aspoutputType=XML&actionType=login&loginType=CHPP&Loginname=" +
                      userName + "&readonlypassword=" + securityCode;
               HttpWebRequest loHttp = loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
               loHttp.UserAgent = myUserAgent;
               loHttp.CookieContainer = new CookieContainer();
               if (CookiesCol != null && CookiesCol.Count > 0)
                   loHttp.CookieContainer.Add(CookiesCol);
               HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
               CookiesCol = loWebResponse.Cookies;
               if (loWebResponse.Cookies.Count > 0)
                   CookiesCol = loWebResponse.Cookies;
               Encoding enc = Encoding.GetEncoding(1252);
               StreamReader loResponseStream = new StreamReade(loWebResponse.GetResponseStream (), enc);
               String lcHtml = loResponseStream.ReadToEnd();
               loWebResponse.Close();
               loResponseStream.Close();
               return lcHtml;            
           }
           catch (Exception e)
           {
               return e.Message;
           }
       }