using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace RoboticsClub { /* * PersonalPage represents all information for a * http://www.personal.psu.edu/ */ class PersonalPage { // Internal error string private bool _ErrorBool = false; public bool ErrorBool { get { return _ErrorBool; } set { _ErrorBool = value; } } private String _ErrorString; public String ErrorString { get { return _ErrorString; } set { _ErrorString = value; } } private String _FullName; public String FullName { get { return _FullName; } set { _FullName = value; } } private String _ID; public String ID { get { return _ID; } set { _ID = value; } } private String _EMail; public String EMail { get { return _EMail; } set { _EMail = value; } } private String _Major; public String Major { get { return _Major; } set { _Major = value; } } private String _Year; public String Year { get { return _Year; } set { _Year = value; } } private static int MAX_LENGTH = 128; // Constructor: Takes a persons student ID public PersonalPage(String UserID) { // Save user id ID = (String)UserID.Clone(); // Load page into internal link String page = LoadPage(UserID); // Parse the page only if it's valid if (page.Length > 500) ParsePage(page); else ErrorBool = true; } // Load user data, returns new page private String LoadPage(String UserID) { // Load page into internal string WebClient Client = new WebClient(); String ReturnedPage = ""; Client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // Attempt to load a page try { // Set the target address for the search engine Uri Address = new Uri("http://www.psu.edu/cgi-bin/ldap/ldap_query.cgi"); // Post target search parameters string PostData = "sn=&cn=&uid=" + UserID + "&mail=&full=0&submit=Search+Directory"; ReturnedPage = Client.UploadString(Address, PostData); } catch (System.Net.WebException Error) { // Post the error ErrorBool = true; ErrorString = Error.Message; } // Return the loaded page return ReturnedPage; } // Parse a given page into user data private void ParsePage(String Page) { // Find the name FullName = CutOut(Page, "\n\n", "\n
\n"); if (FullName.Length > MAX_LENGTH) FullName = "No name found"; // Find the email EMail = CutOut(Page, "@psu.edu\">", "
"); if (EMail.Length > MAX_LENGTH) EMail = "No email found"; // Find the major Major = CutOut(Page, "Curriculum:\n\n\n\n", "\n
"); if (Major.Length > MAX_LENGTH) Major = "No major found"; // Find the year Year = CutOut(Page, "Title:\n\n\n\n", "\n
"); if (Year.Length > MAX_LENGTH) Year = "No year found"; } // Returns text between two specific strings of text private String CutOut(String Text, String BeforeTag, String AfterTag) { int StartLoc = Text.IndexOf(BeforeTag) + BeforeTag.Length; int EndLoc = Text.IndexOf(AfterTag, StartLoc); // Return the substring return Text.Substring(StartLoc, EndLoc - StartLoc); } } }