/// <summary> /// Check if the input is a valid url. /// </summary> /// <param name="url">Url to validate.</param> /// <returns> /// Returns input if it's a valid url. If the input is not a valid url, returns empty string. /// </returns> public static string CleanUrl(string url) { if (string.IsNullOrWhiteSpace(url)) { return(string.Empty); } const string prefix = "http"; if (url.Substring(0, prefix.Length) != prefix) { url = prefix + "://" + url; } using (MyClient client = new MyClient()) { client.HeadOnly = true; try { client.DownloadString(url); } catch (WebException) { url = string.Empty; } return(url); } }
/// <summary> /// Check if the input is a valid url. /// </summary> /// <param name="url"></param> /// <returns>Returns input if it's a valid url. If the input is not a valid url, returns empty string.</returns> public static string CleanUrl(string url) { if(string.IsNullOrWhiteSpace(url)) { return string.Empty; } string prefix = "http"; if(url.Substring(0, prefix.Length) != prefix) { url = prefix + "://" + url; } using(var client = new MyClient()) { client.HeadOnly = true; try { client.DownloadString(url); } catch(WebException) { url = string.Empty; } return url; } }
/// <summary> /// /// </summary> /// <param name="url"></param> /// <param name="brute"></param> /// <returns></returns> public static bool IsURLValid(string url, bool brute = false) { if (!brute) { Uri uri = null; return(Uri.TryCreate(url, UriKind.Absolute, out uri)); } else { // using MyClient from linked post using (var client = new MyClient() { HeadOnly = true }) { try { var s1 = client.DownloadString(url); // no error has occured so, return(true); } catch { return(false); } } } }
/// <summary> /// <see cref="https://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid"/> /// </summary> /// <param name="url"></param> /// <returns></returns> public static bool UrlIsValid(string url) { try { using (var client = new MyClient()) { client.HeadOnly = true; client.DownloadString(url); return(true); } } catch { //either 404 or 503 return(false); } }
static void Main() { using (MyClient client = new MyClient()) { client.HeadOnly = true; string uri = "http://www.google.com"; byte[] body = client.DownloadData(uri); // note should be 0-length string type = client.ResponseHeaders["content-type"]; client.HeadOnly = false; // check 'tis not binary... we'll use text/, but could // check for text/html if (type.StartsWith(@"text/")) { string text = client.DownloadString(uri); Console.WriteLine(text); } } }
/// <summary> /// <see cref="https://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid"/> /// <seealso cref="https://www.c-sharpcorner.com/forums/c-sharp-webclient-throws-error-while-downloading-a-url-string"/> /// </summary> /// <param name="url"></param> /// <returns></returns> public static bool UrlIsValid(string url) { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (var client = new MyClient()) { client.HeadOnly = true; client.DownloadString(url); return(true); } } catch (Exception x) { x.Data.Add("url", url); //either 404 or 503 XLogger.Error(x); return(false); } }
private static string GetWebContentCore5(string url) { string res = ""; try { using (MyClient client = new MyClient()) // WebClient class inherits IDisposable { //client.DownloadFile(url, @"C:\localfile.html"); // Or you can get the file content without saving it res = client.DownloadString(url); } return(res); } catch (Exception x) { x.Data.Add("url", url); throw; } }
private static string GetContent(String csvLink) { if (System.IO.File.Exists(csvLink)) return System.IO.File.ReadAllText(csvLink); String content = ""; using (MyClient client = new MyClient()) { client.HeadOnly = true; string uri = csvLink; byte[] body = client.DownloadData(uri); // note should be 0-length string type = client.ResponseHeaders["content-type"]; client.HeadOnly = false; // check 'tis not binary... we'll use text/, but could // check for text/html if (type.StartsWith(@"text/") || IsTextExtension(csvLink)) { client.HeadOnly = false; content = client.DownloadString(uri); } else throw new Exception(@"Dokumentet skal være tekst-baseret. Se den gratis demo for hjælp"); } return content; }
private void toolStripMenuUpdate_Click(object sender, EventArgs e) { string currentVersionUrlTxt = _YameConst.Updateurl + _YameConst.Updatefilename; using (var client = new MyClient()) { string updateversion = client.DownloadString(currentVersionUrlTxt); if (String.Empty != updateversion) { Version assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; string currentVersion = assemblyVersion.Major.ToString() + assemblyVersion.Minor.ToString() + assemblyVersion.Build.ToString(); string updateVersionFileUrl = _YameConst.Updateurl + _YameConst.Appname + "_" + updateversion + ".zip"; if (updateversion != currentVersion) { string message = "Update " + updateversion + " available !\r\n" + "\r\nVisit website for download ?"; DialogResult result = SMARTDIALOG.SmartDialog.Show(message, "V" + currentVersion, "Server found.", "Update found", "NO", "YES"); if(result == DialogResult.No) // Inverted button on frame ;) System.Diagnostics.Process.Start(_YameConst.Helpurl + "#download"); } else { SMARTDIALOG.SmartDialog.Show("Current version is up to date !", "V"+currentVersion, "Server found.", "No update found", "OK"); } } else { MessageBox.Show("Website unreachable!"); } } }