/// <summary> Check if a resource is a valid URL. /// </summary> /// <param name="resourceLocn">The resource to test. /// </param> /// <returns> <code>true</code> if the resource is a valid URL. /// /// </returns> public static bool IsURL(string resourceLocn) { System.Uri url; bool ret; try { url = SupportClass.CreateUri(resourceLocn); ret = true; } catch (System.UriFormatException) { ret = false; } return(ret); }
public virtual System.Uri ConstructUrl(string link, string baseURL) { string path; bool modified; bool absolute; int index; System.Uri url; // constructed URL combining relative link and base url = new System.Uri(SupportClass.CreateUri(baseURL), link); path = url.AbsolutePath; modified = false; absolute = link.StartsWith("/"); if (!absolute) { // we prefer to fix incorrect relative links // this doesn't fix them all, just the ones at the start while (path.StartsWith("/.")) { if (path.StartsWith("/../")) { path = path.Substring(3); modified = true; } else if (path.StartsWith("/./") || path.StartsWith("/.")) { path = path.Substring(2); modified = true; } else { break; } } } // fix backslashes while (-1 != (index = path.IndexOf("/\\"))) { path = path.Substring(0, (index + 1) - (0)) + path.Substring(index + 2); modified = true; } if (modified) { url = SupportClass.CreateUri(url, path); } return(url); }
/// <summary> Opens a connection based on a given string. /// The string is either a file, in which case <code>file://localhost</code> /// is prepended to a canonical path derived from the string, or a url that /// begins with one of the known protocol strings, i.e. <code>http://</code>. /// Embedded spaces are silently converted to %20 sequences. /// </summary> /// <param name="resource">The name of a file or a url. /// </param> /// <param name="feedback">The object to use for messages or <code>null</code> for no feedback. /// </param> /// <exception cref=""> ParserException if the string is not a valid url or file. /// /// </exception> public static WebRequest OpenConnection(string resource, ParserFeedback feedback) { System.Uri url; WebRequest ret; try { url = SupportClass.CreateUri(LinkProcessor.FixSpaces(resource)); ret = ParserHelper.OpenConnection(url, feedback); } catch (System.UriFormatException ufe) { string msg = "HTMLParser.OpenConnection() : Error in opening a connection to " + resource; ParserException ex = new ParserException(msg, ufe); if (null != feedback) { feedback.Error(msg, ex); } throw ex; } return(ret); }
public int FindImageTagCount() { int imgTagCount = 0; try { System.Uri url = SupportClass.CreateUri(UriToExamine); System.IO.Stream responseStream = System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(); System.IO.StreamReader reader; reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF7); imgTagCount = CountImageTagsWithoutHTMLParser(reader); responseStream.Close(); } catch (System.UriFormatException) { System.Console.Error.WriteLine("URL was malformed!"); } catch (System.IO.IOException) { System.Console.Error.WriteLine("IO Exception occurred while trying to open stream"); } return(imgTagCount); }