Note that there are some explanatory texts on larger screens.

plurals
  1. POA better way to validate URL in C# than try-catch?
    text
    copied!<p>I'm building an application to retrieve an image from internet. Even though it works fine, it is slow (on wrong given URL) when using try-catch statements in the application. </p> <p>(1) Is this the best way to verify URL and handle wrong input - or should I use Regex (or some other method) instead?</p> <p>(2) Why does the application try to find images locally if I don't specify http:// in the textBox?</p> <pre><code>private void btnGetImage_Click(object sender, EventArgs e) { String url = tbxImageURL.Text; byte[] imageData = new byte[1]; using (WebClient client = new WebClient()) { try { imageData = client.DownloadData(url); using (MemoryStream ms = new MemoryStream(imageData)) { try { Image image = Image.FromStream(ms); pbxUrlImage.Image = image; } catch (ArgumentException) { MessageBox.Show("Specified image URL had no match", "Image Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (ArgumentException) { MessageBox.Show("Image URL can not be an empty string", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (WebException) { MessageBox.Show("Image URL is invalid.\nStart with http:// " + "and end with\na proper image extension", "Not a valid URL", MessageBoxButtons.OK, MessageBoxIcon.Information); } } // end of outer using statement } // end of btnGetImage_Click </code></pre> <p><strong>EDIT:</strong> I tried the suggested solution by Panagiotis Kanavos (thank you for your effort!), but it only gets caught in the if-else statement if the user enters <code>http://</code> and nothing more. Changing to UriKind.Absolute catches empty strings as well! Getting closer :) The code as of now:</p> <pre><code>private void btnGetImage_Click(object sender, EventArgs e) { String url = tbxImageURL.Text; byte[] imageData = new byte[1]; Uri myUri; // changed to UriKind.Absolute to catch empty string if (Uri.TryCreate(url, UriKind.Absolute, out myUri)) { using (WebClient client = new WebClient()) { try { imageData = client.DownloadData(myUri); using (MemoryStream ms = new MemoryStream(imageData)) { imageData = client.DownloadData(myUri); Image image = Image.FromStream(ms); pbxUrlImage.Image = image; } } catch (ArgumentException) { MessageBox.Show("Specified image URL had no match", "Image Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (WebException) { MessageBox.Show("Image URL is invalid.\nStart with http:// " + "and end with\na proper image extension", "Not a valid URL", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("The Image Uri is invalid.\nStart with http:// " + "and end with\na proper image extension", "Uri was not created", MessageBoxButtons.OK, MessageBoxIcon.Information); } </code></pre> <p>I must be doing something wrong here. :(</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload