/// <summary> /// Event triggered from manager class, updates all of the web page data on the form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void m_RequestComplete(object sender, RequestCompleteArgs e) { resultDisplay.Text = e.pageData; URLinputBox.Text = e.URL; titleHolder.Text = e.title; HTTPHolder.Text = e.request; }
/// <summary> /// Loads the provided URL (via triggering an event) /// </summary> /// <param name="URL">The URL that has been requested</param> /// <param name="historyItem"> True (yes the item should be added to history) False (do not add to history) </param> public async Task <bool> _loadWebsite(string URL, bool historyItem) { // Setting the currentURL to the last loaded webpage currentURL = URL; HttpResponseMessage httpResponse = await HTTP.Get(URL); if (httpResponse.StatusCode == HttpStatusCode.OK) { browser = new BrowserResponse(httpResponse); result = await browser.getContent(); if (historyItem) { historyManager.addToHistory(URL); } RequestCompleteArgs args = new RequestCompleteArgs(); args.pageData = result; // Used the following resource for the regex //https://www.experts-exchange.com/questions/23135727/regular-expression-to-match-title-tag-on-html-page.html args.title = Regex.Match(result, "<title>([^<]*)</title>").Groups[1].Value; args.request = httpResponse.StatusCode.ToString(); args.URL = currentURL; OnRequestComplete(args); return(true); } else { // Display the relevant HTTPresponse status code error messageS // Error messages from : https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=netcore-3.1 switch (httpResponse.StatusCode) { case HttpStatusCode.BadRequest: System.Windows.Forms.MessageBox.Show("The request could not be understood by the server", "400 Bad Request"); break; case HttpStatusCode.Forbidden: System.Windows.Forms.MessageBox.Show("The server refuses to fulfill the request", "403 Forbidden"); break; case HttpStatusCode.NotFound: System.Windows.Forms.MessageBox.Show("The requested resource does not exist on the server", "404 Not Found"); break; default: // Handling all other status codes (apart from 200) System.Windows.Forms.MessageBox.Show(httpResponse.StatusCode.ToString(), "Unhandeled status code"); break; } } //Throws an exception if invalid response httpResponse.EnsureSuccessStatusCode(); // The webpage was not loaded return(false); }
/// <summary> /// Used to trigger an event in another class when the a page request is completed /// </summary> /// <param name="e">The args passed to the event</param> protected virtual void OnRequestComplete(RequestCompleteArgs e) { EventHandler <RequestCompleteArgs> handler = RequestComplete; if (handler != null) { handler(this, e); } }