/// <summary> /// Download a file from a URL. /// </summary> /// <param name="url">URL of file.</param> /// <param name="name">Name to write file to disk.</param> /// <param name="timeout">Timeout period after file request.</param> /// <returns>true if successful.</returns> public static bool GetFile(String url, String name, int timeout) { try { using (var client = new WebDownload(timeout)) { client.Credentials = new NetworkCredential("username", "password", "domain"); client.Headers.Add(HttpRequestHeader.UserAgent, CrawlUtil.GetUserAgent()); client.DownloadFile(url, name); return(true); } } catch (Exception e) { return(false); } }
/// <summary> /// Get the user's location from a profile. /// </summary> /// <param name="userName">Profile to parse.</param> /// <returns>Location string, if available.</returns> /// <remarks>Shortcut function to avoid parsing entire profile.</remarks> public static String GetUserLocation(String userName) { String profileURL = String.Format(@"https://myspace.com/{0}", userName); var doc = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlNode.ElementsFlags["br"] = HtmlAgilityPack.HtmlElementFlag.Empty; doc.OptionWriteEmptyNodes = true; try { var webRequest = HttpWebRequest.Create(profileURL); ((HttpWebRequest)webRequest).UserAgent = CrawlUtil.GetUserAgent(); Stream stream = webRequest.GetResponse().GetResponseStream(); doc.Load(stream); stream.Close(); return(doc.DocumentNode.SelectSingleNode(@"//div[@id='profileDetails']//div[@id='locAndWeb']//div[@class='location_white location ']")?.Attributes["data-display-text"]?.Value); } catch (Exception e) { } return(null); }